public static Array <Real> TensorDotRight(this Array <Real> mat, Array <Real> vec, Array <Real> result = null, Real beta = 1) { if (vec.Shape.Length != 1) { throw new ArgumentException(); } if (mat.Shape.Length != 2) { throw new ArgumentException(); } var shape = new[] { mat.Shape[0], mat.Shape[1], vec.Shape[0] }; if (result == null) { result = new Array <Real>(shape); } else if (result.Shape.Length != shape.Length) { throw new ArgumentException(); // TODO: check axes } var offsetRes = result.Offset; for (int i = 0; i < vec.Shape[0]; ++i) { // result[_, _, i] = vec[i] * mat + beta * result[_, _, i] if (beta != 1) { Blas.scal(mat.Size, beta, mat.Values, offsetRes, mat.Stride[1]); } Blas.axpy(mat.Size, vec.Item[i], mat.Values, mat.Offset, mat.Stride[1], result.Values, offsetRes, result.Stride[1]); offsetRes += result.Stride[2]; } return(result); }
public static Array <Real> Scale(this Array <Real> a, Real alpha, Array <Real> result = null) { if (result != a) { result = a.Copy(result: result); } Array_.ElementwiseOp(0, result, 0, (n, x, offsetx, incx) => { Blas.scal(n, alpha, x, offsetx, incx); }); return(result); }