コード例 #1
0
        /// <summary>
        ///     Compute the truth value of x1 OR x2 element-wise.
        /// </summary>
        /// <param name="lhs">Input boolean array.</param>
        /// <param name="rhs">Input boolean array.</param>
        /// <returns>Returns True if the arrays are equal.</returns>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.logical_or.html</remarks>
        public static unsafe NDArray <bool> logical_or(NDArray lhs, NDArray rhs)
        {
#if _REGEN1
            if (lhs.typecode != rhs.typecode)
            {
                throw new NotImplementedException("please make sure operands have the same data type");
            }
            else if (lhs.typecode == NPTypeCode.Boolean)
            {
                if (lhs.Shape.IsScalar && rhs.Shape.IsScalar)
                {
                    return(NDArray.Scalar(*(bool *)lhs.Address || *(bool *)rhs.Address).MakeGeneric <bool>());
                }

                var(BroadcastedLeftShape, BroadcastedRightShape) = DefaultEngine.Broadcast(lhs.Shape, rhs.Shape);
                var   lhs_address = (bool *)lhs.Address;
                var   rhs_address = (bool *)rhs.Address;
                var   ret         = new NDArray <bool>(new Shape(BroadcastedLeftShape.dimensions), true);
                Shape retShape    = ret.Shape;

                //iterate
                var   ret_address = (bool *)ret.Address;
                var   incr        = new NDCoordinatesIncrementor(BroadcastedLeftShape.dimensions); //doesn't matter which side it is.
                int[] current     = incr.Index;
                do
                {
                    *(ret_address + retShape.GetOffset(current)) = (*(lhs_address + BroadcastedLeftShape.GetOffset(current))) || *(rhs_address + BroadcastedRightShape.GetOffset(current));
                } while (incr.Next() != null);

                return(ret);
            }
コード例 #2
0
        /// <summary>
        ///     Random values in a given shape.
        ///     Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
        /// </summary>
        public NDArray rand(Shape shape)
        {
            NDArray ret = new NDArray(typeof(double), shape, false);

            unsafe
            {
                var addr = (double *)ret.Address;
                var incr = new ValueCoordinatesIncrementor(ref shape);
                do
                {
                    *(addr + shape.GetOffset(incr.Index)) = randomizer.NextDouble();
                } while (incr.Next() != null);
            }

            return(ret);
        }
コード例 #3
0
        public void SetString(string value, params int[] indices)
        {
            Debug.Assert(typecode == NPTypeCode.Char);

            // ReSharper disable once ReplaceWithStringIsNullOrEmpty
            if (value == null || value.Length == 0)
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(value));
            }

            if (Shape.dimensions.Length - 1 != indices.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(indices), "SetString(string, int[] indices) can only accept coordinates that point to a vector of chars.");
            }

            unsafe
            {
                if (Shape.IsContiguous)
                {
                    var dst = (char *)Address + Shape.GetOffset(indices);
                    fixed(char *src = value)
                    {
                        var len = sizeof(char) * value.Length;

                        Buffer.MemoryCopy(src, dst, len, len);
                    }
                }
                else
                {
                    fixed(char *strChars = value)
                    {
                        SetData(new ArraySlice <char>(new UnmanagedMemoryBlock <char>(strChars, value.Length)), indices);
                    }
                }
            }
        }
コード例 #4
0
        protected void autoresetDefault_Single()
        {
            if (typeof(TOut) == typeof(Single))
            {
                autoresetDefault_NoCast();
                return;
            }

            var   localBlock = Block;
            Shape shape      = Shape;
            var   convert    = Converts.FindConverter <Single, TOut>();

            if (!Shape.IsContiguous || Shape.ModifiedStrides)
            {
                //Shape is sliced, auto-resetting
                switch (Type)
                {
                case IteratorType.Scalar:
                {
                    var offset = shape.TransformOffset(0);
                    if (offset != 0)
                    {
                        MoveNext          = () => convert(*((Single *)localBlock.Address + offset));
                        MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    }
                    else
                    {
                        MoveNext          = () => convert(*((Single *)localBlock.Address));
                        MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    }

                    Reset   = () => { };
                    HasNext = () => true;
                    break;
                }

                case IteratorType.Vector:
                {
                    var size = Shape.size;
                    MoveNext = () =>
                    {
                        var ret = convert(*((Single *)localBlock.Address + shape.GetOffset(index++)));
                        if (index >= size)
                        {
                            index = 0;
                        }
                        return(ret);
                    };
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");

                    Reset   = () => index = 0;
                    HasNext = () => true;
                    break;
                }

                case IteratorType.Matrix:
                case IteratorType.Tensor:
                {
                    var iterator = new NDCoordinatesIncrementor(ref shape, incr => incr.Reset());
                    var index    = iterator.Index;
                    Func <int[], int> getOffset = shape.GetOffset;
                    MoveNext = () =>
                    {
                        var ret = convert(*((Single *)localBlock.Address + getOffset(index)));
                        iterator.Next();
                        return(ret);
                    };
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => iterator.Reset();
                    HasNext           = () => true;
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                //Shape is not sliced, auto-resetting
                switch (Type)
                {
                case IteratorType.Scalar:
                    MoveNext          = () => convert(*(Single *)localBlock.Address);
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => { };
                    HasNext           = () => true;
                    break;

                case IteratorType.Vector:
                    var size = Shape.size;
                    MoveNext = () =>
                    {
                        var ret = convert(*((Single *)localBlock.Address + index++));
                        if (index >= size)
                        {
                            index = 0;
                        }
                        return(ret);
                    };
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => index = 0;
                    HasNext           = () => true;
                    break;

                case IteratorType.Matrix:
                case IteratorType.Tensor:
                    var iterator = new NDOffsetIncrementorAutoresetting(Shape);     //we do not copy the dimensions because there is not risk for the iterator's shape to change.
                    MoveNext          = () => convert(*((Single *)localBlock.Address + iterator.Next()));
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    HasNext           = () => true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
コード例 #5
0
        protected void setDefaults_UInt32() //UInt32 is the input type
        {
            if (AutoReset)
            {
                autoresetDefault_UInt32();
                return;
            }

            if (typeof(TOut) == typeof(UInt32))
            {
                setDefaults_NoCast();
                return;
            }

            var convert = Converts.FindConverter <UInt32, TOut>();

            //non auto-resetting.
            var   localBlock = Block;
            Shape shape      = Shape;

            if (Shape.IsSliced || Shape.IsBroadcasted)
            {
                //Shape is sliced, not auto-resetting
                switch (Type)
                {
                case IteratorType.Scalar:
                {
                    var hasNext = new Reference <bool>(true);
                    var offset  = shape.TransformOffset(0);

                    if (offset != 0)
                    {
                        MoveNext = () =>
                        {
                            hasNext.Value = false;
                            return(convert(*((UInt32 *)localBlock.Address + offset)));
                        };
                        MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    }
                    else
                    {
                        MoveNext = () =>
                        {
                            hasNext.Value = false;
                            return(convert(*((UInt32 *)localBlock.Address)));
                        };
                        MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    }

                    Reset   = () => hasNext.Value = true;
                    HasNext = () => hasNext.Value;
                    break;
                }

                case IteratorType.Vector:
                {
                    MoveNext          = () => convert(*((UInt32 *)localBlock.Address + shape.GetOffset(index++)));
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => index = 0;
                    HasNext           = () => index < Shape.size;
                    break;
                }

                case IteratorType.Matrix:
                case IteratorType.Tensor:
                {
                    var hasNext  = new Reference <bool>(true);
                    var iterator = new NDCoordinatesIncrementor(ref shape, _ => hasNext.Value = false);
                    Func <int[], int> getOffset = shape.GetOffset;
                    var index = iterator.Index;

                    MoveNext = () =>
                    {
                        var ret = convert(*((UInt32 *)localBlock.Address + getOffset(index)));
                        iterator.Next();
                        return(ret);
                    };
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");

                    Reset = () =>
                    {
                        iterator.Reset();
                        hasNext.Value = true;
                    };

                    HasNext = () => hasNext.Value;
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                //Shape is not sliced, not auto-resetting
                switch (Type)
                {
                case IteratorType.Scalar:
                    var hasNext = new Reference <bool>(true);
                    MoveNext = () =>
                    {
                        hasNext.Value = false;
                        return(convert(*((UInt32 *)localBlock.Address)));
                    };
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => hasNext.Value = true;
                    HasNext           = () => hasNext.Value;
                    break;

                case IteratorType.Vector:
                    MoveNext          = () => convert(*((UInt32 *)localBlock.Address + index++));
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => index = 0;
                    HasNext           = () => index < Shape.size;
                    break;

                case IteratorType.Matrix:
                case IteratorType.Tensor:
                    var iterator = new NDOffsetIncrementor(Shape);     //we do not copy the dimensions because there is not risk for the iterator's shape to change.
                    MoveNext          = () => convert(*((UInt32 *)localBlock.Address + iterator.Next()));
                    MoveNextReference = () => throw new NotSupportedException("Unable to return references during iteration when casting is involved.");
                    Reset             = () => iterator.Reset();
                    HasNext           = () => iterator.HasNext;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }