public NArray <int> RightShift(NArray <int> operand, int shift) { var result = NewNArrayLike(operand); Provider(operand).RightShift(operand, shift, result); return(result); }
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); }
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); }
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); }
public static void AssertVectorsOfEqualLength(NArray a, NArray b) { if (!(a.IsVector && b.IsVector && a.Length == b.Length)) { throw new ArgumentException("not equal length vectors"); } }
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); }
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); }
public static void AssertIsColumn(NArray column, string paramName) { if (column.ColumnCount != 1) { throw new ArgumentException("is not a column", paramName); } }
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"); } }
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); }
//#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); }
public static NArray CholeskyDecomposition(NArray a) { var result = a.Clone(MatrixRegion.LowerTriangle); ExecutionContext.Executor.CholeskyDecomposition(result); return(result); }
public static void AssertIsRow(NArray row, string paramName) { if (row.RowCount != 1) { throw new ArgumentException("is not a row", paramName); } }
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); }
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); }
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)); } }
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)); } }
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); } }
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))); }
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; }
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); } }
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); } }
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>()); }
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)); }
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)); } }
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)); }
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); }
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); }
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); }
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)); }