コード例 #1
0
        public NArray <int> RightShift(NArray <int> operand, int shift)
        {
            var result = NewNArrayLike(operand);

            Provider(operand).RightShift(operand, shift, result);
            return(result);
        }
コード例 #2
0
        public override NArray <T> UnaryElementWiseOperation <T>(NArray <T> operand,
                                                                 UnaryElementWiseOperation operation)
        {
            NArray <T> result = null;

            if (operand.IsScalar)
            {
                Func <T, T> scalarOperation = null;
                switch (operation)
                {
                case VectorAccelerator.UnaryElementWiseOperation.Negate:
                    scalarOperation = (op) => { T res; Negate <T>(op, out res); return(res); };
                    break;
                //case VectorAccelerator.UnaryElementWiseOperation.Exp:

                default:
                    throw new NotImplementedException();
                }
                if (!IsIndependentVariable(operand))
                {
                    return(NewScalarNArray(scalarOperation(operand.First())));
                }
                else
                {
                    result = NewScalarLocalNArray(scalarOperation(operand.First()), true);
                }
            }
            else
            {
                result = NewNArrayLike(operand);
            }
            DoUnaryElementWiseOperation <T>(operand, result, operation);

            return(result);
        }
コード例 #3
0
        private NArray <T> BinaryElementWiseOperation <T>(NArray <T> operand1, NArray <T> operand2,
                                                          ExpressionType type, Func <T, T, T> scalarOperation)
        {
            NArray <T> result = null;

            if (operand1.IsScalar && operand2.IsScalar)
            {
                if (!IsIndependentVariable(operand1) && !IsIndependentVariable(operand2))
                {
                    return(NewScalarNArray(scalarOperation(operand1.First(), operand2.First()))); // this is a scalar that does not depend on any independent variable: can simply return
                }
                else
                {
                    result = NewScalarLocalNArray(scalarOperation(operand1.First(), operand2.First()), true);
                }
            }
            else if (operand1.IsScalar)
            {
                result = NewNArrayLike(operand2);
            }
            else
            {
                result = NewNArrayLike(operand1);
            }

            DoBinaryElementWiseOperation(operand1, operand2, result, type);

            return(result);
        }
コード例 #4
0
        public override NArray <T> ElementWiseAdd <T>(NArray <T> operand1, NArray <T> operand2) // immediate-mode version
        {
            NArray <T> result = null;

            if (operand1.IsScalar && operand2.IsScalar)
            {
                result = NewScalarNArray(Add(operand1.First(), operand2.First()));
            }
            else if (!operand1.IsScalar && !operand2.IsScalar)
            {
                result = NewNArrayLike(operand1);
                DoBinaryElementWiseOperation(operand1, operand2, result, ExpressionType.Add);
            }
            else
            {
                T scale; Convert(1, out scale);
                if (operand1.IsScalar)
                {
                    result = NewNArrayLike(operand2);
                    DoScaleOffset(operand2, scale, operand1.First(), result);
                }
                else
                {
                    result = NewNArrayLike(operand1);
                    DoScaleOffset(operand1, scale, operand2.First(), result);
                }
            }
            return(result);
        }
コード例 #5
0
 public static void AssertVectorsOfEqualLength(NArray a, NArray b)
 {
     if (!(a.IsVector && b.IsVector && a.Length == b.Length))
     {
         throw new ArgumentException("not equal length vectors");
     }
 }
コード例 #6
0
        public void Assign <T>(NArray <T> operand1, Func <NArray <T> > operand2, Func <NArrayBool> condition)
        {
            // simple assignment: we evaluate the functions to generate full vectors and assign a portion.
            var op2 = operand2();

            Provider(operand1, op2).Assign(op2, condition(), operand1);
        }
コード例 #7
0
        public void Assign <T>(NArray <T> operand1, NArray <T> operand2)
        {
            var managedStorage = operand2.Storage as ManagedStorage <T>;

            operand1.Storage = new ManagedStorage <T>((T[])managedStorage.Array.Clone(),
                                                      managedStorage.ArrayStart, managedStorage.Length);
        }
コード例 #8
0
 public static void AssertIsColumn(NArray column, string paramName)
 {
     if (column.ColumnCount != 1)
     {
         throw new ArgumentException("is not a column", paramName);
     }
 }
コード例 #9
0
 public static void AssertSameShape <T, S>(NArray <T> a, NArray <S> b)
 {
     if (a.RowCount != b.RowCount || a.ColumnCount != b.ColumnCount)
     {
         throw new ArgumentException("shape mismatch");
     }
 }
コード例 #10
0
        public override NArray <T> ElementWiseSubtract <T>(NArray <T> operand1, NArray <T> operand2)
        {
            NArray <T> result = null;

            if (operand1.IsScalar && operand2.IsScalar)
            {
                result = NewScalarNArray(Subtract(operand1.First(), operand2.First()));
            }
            else if (!operand1.IsScalar && !operand2.IsScalar)
            {
                result = NewNArrayLike(operand1);
                DoBinaryElementWiseOperation(operand1, operand2, result, ExpressionType.Subtract);
            }
            else
            {
                if (operand1.IsScalar)
                {
                    result = NewNArrayLike(operand2);
                    T scale; Convert(-1, out scale);
                    DoScaleOffset(operand2, scale, operand1.First(), result);
                }
                else
                {
                    result = NewNArrayLike(operand1);
                    T scale; Convert(1, out scale);
                    T offset; Negate(operand2.First(), out offset);
                    DoScaleOffset(operand1, scale, offset, result);
                }
            }
            return(result);
        }
コード例 #11
0
        //#region Unary Operations

        public virtual NArray <T> UnaryElementWiseOperation <T>(NArray <T> operand, UnaryElementWiseOperation operation)
        {
            var result = NewNArrayLike(operand);

            DoUnaryElementWiseOperation <T>(operand, result, operation);
            return(result);
        }
コード例 #12
0
        public static NArray CholeskyDecomposition(NArray a)
        {
            var result = a.Clone(MatrixRegion.LowerTriangle);

            ExecutionContext.Executor.CholeskyDecomposition(result);
            return(result);
        }
コード例 #13
0
 public static void AssertIsRow(NArray row, string paramName)
 {
     if (row.RowCount != 1)
     {
         throw new ArgumentException("is not a row", paramName);
     }
 }
コード例 #14
0
        public NArray <T> Index <T>(NArray <T> operand, NArrayInt indices)
        {
            var result = NewNArrayLike <T, int>(indices);

            Provider(operand).Index <T>(operand, indices, result);
            return(result);
        }
コード例 #15
0
        public override NArray <T> ElementWiseMultiply <T>(NArray <T> operand1, NArray <T> operand2)
        {
            NArray <T> result = null;

            if (operand1.IsScalar && operand2.IsScalar)
            {
                result = NewScalarNArray(Multiply(operand1.First(), operand2.First()));
            }
            else if (!operand1.IsScalar && !operand2.IsScalar)
            {
                result = NewNArrayLike(operand1.Length > operand2.Length ? operand1 : operand2);
                DoBinaryElementWiseOperation(operand1, operand2, result, ExpressionType.Multiply);
            }
            else
            {
                T offset; Convert(0, out offset);
                if (operand1.IsScalar)
                {
                    result = NewNArrayLike(operand2);
                    DoScaleOffset(operand2, operand1.First(), offset, result);
                }
                else
                {
                    result = NewNArrayLike(operand1);
                    DoScaleOffset(operand1, operand2.First(), offset, result);
                }
            }
            return(result);
        }
コード例 #16
0
 public static void AssertRowMatchesMatrix(NArray matrix, NArray row, string matrixParam, string rowParam)
 {
     AssertIsRow(row, rowParam);
     if (row.ColumnCount != matrix.ColumnCount)
     {
         throw new ArgumentException(string.Format("mismatching row {0} and matrix {1}", rowParam, matrixParam));
     }
 }
コード例 #17
0
 public static void AssertColumnMatchesMatrix(NArray matrix, NArray column, string matrixParam, string columnParam)
 {
     AssertIsColumn(column, columnParam);
     if (column.RowCount != matrix.RowCount)
     {
         throw new ArgumentException(string.Format("mismatching column {0} and matrix {1}", columnParam, matrixParam));
     }
 }
コード例 #18
0
 public static void AssertIsVectorOfLength(NArray vector, int length, string paramName)
 {
     if (!vector.IsVector || vector.Length != length)
     {
         throw new ArgumentException(
                   string.Format("is not a vector of length {0}", length), paramName);
     }
 }
コード例 #19
0
        public static double Correlation(NArray a, NArray b)
        {
            var aSum = a.Sum();
            var bSum = b.Sum();
            var n    = a.Length;

            return((NMath.Dot(a, b) * n - aSum * bSum)
                   / Math.Sqrt((NMath.Dot(a, a) * n - aSum * aSum) * (NMath.Dot(b, b) * n - bSum * bSum)));
        }
コード例 #20
0
        public static void BlackScholesD1D2Parameters(NArray forward, double strike, NArray volatility,
                                                      double deltaTime, out NArray d1, out NArray d2)
        {
            var volMultSqrtDTime = volatility * Math.Sqrt(deltaTime);

            d1 = (NMath.Log(forward / strike) + (0.5 * volatility * volatility * deltaTime))
                 / volMultSqrtDTime;

            d2 = d1 - volMultSqrtDTime;
        }
コード例 #21
0
 public virtual void ElementWiseAddInPlace <T>(NArray <T> operand1, NArray <T> operand2)
 {
     if (operand2.IsScalar)
     {
         T scale; Convert(1, out scale);
         DoScaleOffset(operand1, scale, operand2.First(), operand2);
     }
     else
     {
         DoBinaryElementWiseOperation(operand1, operand2, operand1, ExpressionType.Add);
     }
 }
コード例 #22
0
        public static StorageLocation GetStorageLocation <T>(NArray <T> operand)
        {
            var storageType = operand.Storage.GetType();

            if (storageType == typeof(ManagedStorage <T>))
            {
                return(StorageLocation.Host);
            }
            else
            {
                return(StorageLocation.Device);
            }
        }
コード例 #23
0
        private ILocalNArray CreateLocalLike <S, T>(NArray <T> array)
        {
            if (_builder.VectorLength == -1)
            {
                _builder.VectorLength = array.Length;
            }
            if (array.Length != _builder.VectorLength)
            {
                throw new ArgumentException("length mismatch", "array");
            }

            return(_builder.CreateLocal <T>());
        }
コード例 #24
0
        public NArray <T> Index <T>(NArray <T> operand, NArrayInt indices)
        {
            if (_builder.VectorLength == -1)
            {
                _builder.VectorLength = indices.Length;
            }
            if (indices.Length != _builder.VectorLength)
            {
                throw new ArgumentException("length mismatch", "array");
            }

            return(NewNArrayLike <T>(operand));
        }
コード例 #25
0
        public static IEnumerable <double> Percentiles(NArray a, IEnumerable <double> percentiles)
        {
            var clone = a.Clone();

            SortInPlace(clone);
            // the ith element is percentile p = 100 * (i + 0.5) / n
            // i = n * p / 100 - 0.5
            var fractionalIndices = percentiles.Select(p => clone.Length * p / 100.0 - 0.5);

            foreach (var fractionalIndex in fractionalIndices)
            {
                var lower       = (int)fractionalIndex;
                var weightUpper = fractionalIndex - lower;
                yield return(clone.GetValue(lower) * (1 - weightUpper) + weightUpper * clone.GetValue(lower + 1));
            }
        }
コード例 #26
0
        public static NArray BlackScholes(double callPutFactor, NArray forward, double strike,
                                          NArray volatility, double deltaTime)
        {
            NArray d1, d2;

            if (deltaTime == 0)
            {
                return(callPutFactor * (forward - strike));
            }

            BlackScholesD1D2Parameters(forward, strike, volatility, deltaTime, out d1, out d2);

            var nd1 = NMath.CumulativeNormal(callPutFactor * d1);
            var nd2 = NMath.CumulativeNormal(callPutFactor * d2);

            return(callPutFactor * (forward * nd1 - strike * nd2));
        }
コード例 #27
0
        public NArrayBool RelativeOperation(NArray operand1, NArray operand2, RelativeOperator op)
        {
            if (operand1.IsScalar)
            {
                throw new ArgumentException();
            }
            var result = NArrayFactory.CreateLike <bool, double>(operand1) as NArrayBool;

            if (operand2.IsScalar)
            {
                Provider(operand1, operand2).RelativeOperation(operand1, operand2.First(), result, op);
            }
            else
            {
                Provider(operand1, operand2).RelativeOperation(operand1, operand2, result, op);
            }
            return(result);
        }
コード例 #28
0
        public NArrayBool RelativeOperation <T>(NArray <T> operand1, NArray <T> operand2, RelativeOperator op)
        {
            if (operand1.IsScalar)
            {
                throw new ArgumentException();
            }
            var result = NewNArrayLike <bool, T>(operand1) as NArrayBool;

            if (operand2.IsScalar)
            {
                ElementWise <T>(operand1).RelativeOperation(operand1, operand2.First(), result, op);
            }
            else
            {
                ElementWise <T>(operand1, operand2).RelativeOperation(operand1, operand2, result, op);
            }
            return(result);
        }
コード例 #29
0
        public override NArray <T> ElementWiseDivide <T>(NArray <T> operand1, NArray <T> operand2)
        {
            NArray <T> result = null;

            if (operand1.IsScalar && operand2.IsScalar)
            {
                result = NewScalarNArray(Divide(operand1.First(), operand2.First()));
            }
            else if (!operand1.IsScalar && !operand2.IsScalar)
            {
                result = NewNArrayLike(operand1);
                DoBinaryElementWiseOperation(operand1, operand2, result, ExpressionType.Divide);
            }
            else
            {
                T offset; Convert(0, out offset);
                if (operand1.IsScalar)
                {
                    result = NewNArrayLike(operand2);
                    if (typeof(T) == typeof(double))
                    {
                        DoScaleInverse(operand2, operand1.First(), result);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    result = NewNArrayLike(operand1);
                    if (typeof(T) == typeof(double))
                    {
                        DoScaleOffset(operand1 as NArray, 1.0 / (operand2 as NArray).First(), 0, (result as NArray));
                    }
                    else if (typeof(T) == typeof(int))
                    {
                        throw new NotImplementedException();
                    }
                }
            }
            return(result);
        }
コード例 #30
0
 public static NArray BlackScholes(CallPut callPutFactor, NArray forward, double strike,
                                   NArray volatility, double deltaTime)
 {
     return(BlackScholes(callPutFactor == CallPut.Call ? 1 : -1, forward, strike, volatility, deltaTime));
 }