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 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 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); }
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> 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); }
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 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 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); }