private static NdArray <T> Dot2x2 <T>(IBufferNdArrayImpl <T> x, IBufferNdArrayImpl <T> yT) { // x .Shape = [m, p] // yT.Shape = [n, p] // retval.Shape = [m, n] var p = x.Shape[1]; Guard.AssertShapeMatch( p == yT.Shape[1], $"x.Shape[1] = {p}, y.Shape[0] = {yT.Shape[1]}"); var m = x.Shape[0]; var n = yT.Shape[0]; var resImpl = new RawNdArrayImpl <T>(new IndexArray(m, n)); var xBuff = x.Buffer; var yTBuff = yT.Buffer; var resBuff = resImpl.Buffer.Span; for (var i = 0; i < m; ++i) { for (var j = 0; j < n; ++j) { resBuff[n * i + j] = VectorOperation.Dot(xBuff.Slice(i * p, p), yTBuff.Slice(j * p, p)); } } return(new NdArray <T>(resImpl)); }
public static bool TryGetBufferImpl <T>(this INdArray <T> array, [NotNullWhen(true)] out IBufferNdArrayImpl <T>?result) { if ((array as INdArrayInternal <T>)?.Entity is IBufferNdArrayImpl <T> xresult) { result = xresult; return(true); } result = default; return(false); }
/// <summary> /// [Pure] Projects each element of a NdArray into a new form. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ndarray"> A NdArray for values to invoke a transform function on. </param> /// <param name="selector"> A transform function to apply to each element. </param> /// <returns> [<c>$ReturnValue.Shape == NdArray.Shape</c>] </returns> public static NdArray <T> Select <T>( this NdArray <T> ndarray, Expression <Func <T, T> > selector) where T : unmanaged { Guard.AssertArgumentNotNull(ndarray, nameof(ndarray)); Guard.AssertArgumentNotNull(selector, nameof(selector)); ReadOnlyMemory <T> source = ndarray.Entity switch { IBufferNdArrayImpl <T> romProvider => romProvider.Buffer, _ => default(ReadOnlyMemory <T>?), } ?? throw new NotSupportedException();
/// <summary> /// [Pure] Applies selector against each corresponding element set of input NdArrays. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ndarray1"></param> /// <param name="ndarray2"> [<c>ndarray1.Shape == ndarray2.Shape</c>] </param> /// <param name="selector"></param> /// <returns></returns> public static NdArray <T> Zip <T>(this NdArray <T> ndarray1, NdArray <T> ndarray2, Expression <Func <T, T, T> > selector ) where T : unmanaged { ReadOnlyMemory <T> getSource(NdArray <T> ndarray) => ndarray.Entity switch { IBufferNdArrayImpl <T> romProvider => romProvider.Buffer, _ => default(ReadOnlyMemory <T>?), }
private static NdArray <T> Dot1x1 <T>(IBufferNdArrayImpl <T> x, IBufferNdArrayImpl <T> y) { // x.Shape = [p] // y.Shape = [p] // retval.Shape = [1] var p = x.Shape[0]; Guard.AssertShapeMatch(p == y.Shape[0], $"x.Shape[0] = {p}, y.Shape[0] = {y.Shape[0]}"); var resImpl = new RawNdArrayImpl <T>(new IndexArray(1)); var xBuff = x.Buffer; var yBuff = y.Buffer; var resBuff = resImpl.Buffer.Span; resBuff[0] = VectorOperation.Dot(xBuff, yBuff); return(new NdArray <T>(resImpl)); }
private static NdArray <T> Dot2x1 <T>(IBufferNdArrayImpl <T> x, IBufferNdArrayImpl <T> y) { // x.Shape = [m, p] // y.Shape = [p] // retval.Shape = [m] var p = x.Shape[1]; Guard.AssertShapeMatch(p == y.Shape[0], $"x.Shape[1] = {p}, y.Shape[0] = {y.Shape[0]}"); var m = x.Shape[0]; var resImpl = new RawNdArrayImpl <T>(new IndexArray(m)); var xBuff = x.Buffer; var yBuff = y.Buffer; var resBuff = resImpl.Buffer.Span; for (var i = 0; i < m; ++i) { resBuff[i] = VectorOperation.Dot(xBuff.Slice(i * p, p), yBuff); } return(new NdArray <T>(resImpl)); }