Exemplo n.º 1
0
 public static Array <Real> Mul(this Array <Real> a, Array <Real> b, Array <Real> result = null)
 {
     return(Array_.ElementwiseOp(a, b, result,
                                 (n, x, offsetx, incx, y, offsety, incy, z, offsetz, incz) =>
     {
         if (incx == 1 && incy == 1 && incz == 1)
         {
             Blas.vmul(n, x, offsetx, y, offsety, z, offsetz);
         }
         else if (incx == 0)   // x[offsetx] is broadcast: y[:] * x[offsetx]
         {
             Blas.gemv(Order.RowMajor, Transpose.NoTrans, n, 1, 1, y, offsety, incy, x, offsetx, 1, 0, z, offsetz, incz);
         }
         //else if (incy == 0)   // y[offsety] is broadcast: x[:] * y[offsety]
         //    Blas.gemv(Order.RowMajor, Transpose.NoTrans, n, 1, 1, x, offsetx, incx, y, offsety, 1, 0, z, offsetz, incz);
         else    // when everything else fails, fallback to slow version
         {
             for (int i = 0; i < n; i++)
             {
                 z[offsetz] = x[offsetx] * y[offsety];
                 offsetx += incx;
                 offsety += incy;
                 offsetz += incz;
             }
         }
     }));
 }
Exemplo n.º 2
0
        /// <summary>
        /// The basic Combine version
        /// result = alpha * (T . x) . y + beta * result
        /// </summary>
        public static Array <Real> Combine21(this Array <Real> t, Array <Real> x, Array <Real> y, Array <Real> result = null, Real alpha = 1, Real beta = 0)
        {
            if (t.Shape[2] != x.Shape[0] && t.Shape[1] != y.Shape[0])
            {
                throw new ArgumentException();
            }
            if (t.Stride[2] != 1)
            {
                throw new NotImplementedException();
            }

            int offsetT = t.Offset;

            if (result == null)
            {
                result = new Array <Real>(t.Shape[0]);
            }
            else
            {
                result.Scale(beta, result: result);
            }

            int offsetRes = result.Offset;
            int strideRes = result.Stride[0];

            int offsetX = x.Offset;
            int strideX = x.Stride[0];

            int strideJ = y.Stride[0];
            int J       = y.Shape[0] * strideJ + y.Offset;

            for (int j = y.Offset; j < J; j += strideJ)
            {
                // result += alpha * y[j] * (t[:, j, :] . x)
                Blas.gemv(Order.RowMajor, Transpose.NoTrans,
                          t.Shape[0], t.Shape[2],
                          alpha * y.Values[j],
                          t.Values, offsetT, t.Stride[0],
                          x.Values, offsetX, strideX,
                          1,
                          result.Values, offsetRes, strideRes
                          );
                offsetT += t.Stride[1];
            }
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// A modified Combine version.
        /// result = alpha * z . (y . T) + beta * result
        /// </summary>
        /// <param name="t">The tensor used to combine y and z: t.Shape = [k, j, i]</param>
        /// <param name="y">A vector: y.Shape = [j]</param>
        /// <param name="z">A vector: z.Shape = [k]</param>
        /// <param name="result">A vector: result.Shape = [i], if null, will be created.</param>
        /// <returns>Returns result.</returns>
        public static Array <Real> Combine10(this Array <Real> t, Array <Real> y, Array <Real> z, Array <Real> result = null, Real alpha = 1, Real beta = 0)
        {
            if (t.Shape.Length != 3 && y.Shape.Length != 1 && z.Shape.Length != 1)
            {
                throw new ArgumentException();
            }
            if (t.Shape[1] != y.Shape[0] && t.Shape[0] != z.Shape[0])
            {
                throw new ArgumentException();
            }
            if (t.Stride[2] != 1)
            {
                throw new NotImplementedException();
            }

            if (result == null)
            {
                result = new Array <Real>(t.Shape[2]);
            }
            else
            {
                result.Scale(beta, result: result);
            }

            int strideK = z.Stride[0];
            int K       = z.Shape[0] * strideK + z.Offset;

            int strideT = t.Stride[0];
            int offsetT = t.Offset;

            for (int k = z.Offset; k < K; k += strideK)
            {
                // result += alpha * z[k] * (y . t[k, :, :])
                Blas.gemv(Order.RowMajor, Transpose.Trans, t.Shape[1], t.Shape[2],
                          alpha * z.Values[k],
                          t.Values, offsetT, t.Stride[1],
                          y.Values, y.Offset, y.Stride[0],
                          1,
                          result.Values, result.Offset, result.Stride[0]);
                offsetT += t.Stride[0];
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Matrix multiplication.
        /// Returns: alpha * dot(a, b) + beta * result
        /// Ie with default value: dot(a, b)
        /// </summary>
        /// <remarks>
        /// For 2-D arrays it is equivalent to matrix multiplication,
        /// and for 1-D arrays to inner product of vectors (without complex conjugation).
        /// For N dimensions it is a sum product over the last axis of a and the second-to-last of b:
        /// dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
        /// `TensorDot` provides more control for N-dim array multiplication.
        /// </remarks>
        public static Array <Real> Dot(this Array <Real> a, Array <Real> b, Array <Real> result = null, Real alpha = 1, Real beta = 0, bool transA = false, bool transB = false)
        {
            if (transA)
            {
                if (a.Shape.Length == 1)
                {
                    if (b.Shape.Length == 1)
                    {
                        if (transB)
                        {
                            return(a.Dot(b, result, alpha, beta));
                        }
                        else
                        {
                            return(a.VectorDot(b));
                        }
                    }
                    if (b.Shape.Length != 2)
                    {
                        throw new NotImplementedException();
                    }
                    return(b.Dot(a, result, alpha, beta, transA: !transB, transB: false));
                }
                else
                {
                    a = a.T;    // TODO: optimize => avoid creating new shape, stride, ...
                    //if (b.Shape.Length == 1 && !transB) b = b.Reshape(1, b.Shape[0]);
                }
                transA = false;
            }

            if (transB)
            {
                if (b.Shape.Length == 1)
                {
                    if (a.Shape.Length == 1)
                    {
                        if (transA)
                        {
                            throw new NotImplementedException();
                        }
                        return(a.Outer(b, result: result, alpha: alpha, beta: 0));
                    }
                    throw new NotImplementedException();
                    //if (a.Shape.Length != 2) throw new NotImplementedException();
                    //if (a.IsTransposed())
                    //{
                    //    a = a.T;
                    //    transA = !transA;
                    //}
                    //result = new Array<Real>(transA ? a.Shape[1] : a.Shape[0], b.Shape[0]);   // TODO: result != null
                    //Blas.gemm(Order.RowMajor, transA ? Transpose.Trans : Transpose.NoTrans, Transpose.NoTrans,
                    //    result.Shape[0], result.Shape[1], 1,
                    //    alpha,
                    //    a.Values, a.Offset, a.Stride[0],
                    //    b.Values, b.Offset, b.Stride[0],
                    //    beta,
                    //    result.Values, result.Offset, result.Stride[0]);
                    //return result;
                }
                else
                {
                    b = b.T;    // TODO: optimize => avoid creating new shape, stride, ...
                }
                transB = false;
            }

            // TODO: check alpha & beta
            if (a.Shape.Length == 0)
            {
                return(b.Scale(a.Values[a.Offset]));
            }
            if (b.Shape.Length == 0)
            {
                return(a.Scale(b.Values[b.Offset]));
            }
            if (a.Shape.Length == 1)     // vector x tensor
            {
                if (b.Shape.Length == 1) // vector x vector
                {
                    if (a.Shape[0] != b.Shape[0])
                    {
                        throw AssertArray.BadRank("objects are not aligned: [{0}] dot [{1}]", a.Shape[0], b.Shape[0]);
                    }
                    if (result == null)
                    {
                        result = new Array <Real>();
                    }
                    else
                    {
                        if (result.Shape.Length != 0)
                        {
                            throw AssertArray.BadRank("objects are not aligned");
                        }
                    }

                    result.Values[result.Offset] = beta * result.Values[result.Offset] + alpha * Blas.dot(a.Shape[0], a.Values, a.Offset, a.Stride[0], b.Values, b.Offset, b.Stride[0]);
                    return(result);
                }
                else if (b.Shape.Length == 2)   // vector x matrix
                {
                    if (a.Shape[0] != b.Shape[0])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(b.Shape[1]);
                    }
                    else
                    {
                        if (result.Shape.Length != 1)
                        {
                            throw new RankException("objects are not aligned");
                        }
                        if (result.Shape[0] != b.Shape[1])
                        {
                            throw new RankException("objects are not aligned");
                        }
                    }

                    // dgemv computes matrix x vector => result = M.T.dot(v.T).T
                    transB = !transB;
                    if (b.IsTransposed())
                    {
                        transB = !transB;
                        b      = b.T;
                    }
                    // y:= alpha * A' * x + beta * y
                    Blas.gemv(Order.RowMajor, transB ? Transpose.Trans : Transpose.NoTrans, b.Shape[0], b.Shape[1], alpha,
                              b.Values, b.Offset, b.Stride[0],
                              a.Values, a.Offset, a.Stride[0],
                              beta,
                              result.Values, result.Offset, result.Stride[0]);
                    return(result);
                }
                else if (b.Shape.Length == 3) // vector x tensor3
                {
                    // TODO: beta ?
                    if (a.Shape[0] != b.Shape[1])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(b.Shape[0], b.Shape[2]);
                    }
                    else
                    {
                        if (result.Shape[0] != b.Shape[0])
                        {
                            throw new RankException("objects are not aligned");
                        }
                        if (result.Shape[1] != b.Shape[2])
                        {
                            throw new RankException("objects are not aligned");
                        }
                    }

                    var offsetk = b.Offset;
                    var k_0     = result.Offset;
                    for (var k = 0; k < result.Shape[0]; k++)       // result.Shape[0] == b.Shape[0]
                    {
                        var offsetm = offsetk;
                        var k_m     = k_0;
                        for (var m = 0; m < result.Shape[1]; m++)                                                                               // result.Shape[1] == b.Shape[2]
                        {
                            result.Values[k_m] = alpha * Blas.dot(a.Shape[0], a.Values, a.Offset, a.Stride[0], b.Values, offsetm, b.Stride[1]); // a.Shape[axis] == b.Shape[1];
                            offsetm           += b.Stride[2];
                            k_m += result.Stride[1];
                        }
                        offsetk += b.Stride[0];
                        k_0     += result.Stride[0];
                    }

                    return(result);
                }
                throw new NotImplementedException();
            }
            else if (b.Shape.Length == 1)   // tensor x vector
            {
                if (a.Shape.Length == 2)    // matrix x vector
                {
                    if (a.Shape[1] != b.Shape[0])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(a.Shape[0]);
                    }
                    else
                    {
                        if (result.Shape.Length != b.Shape.Length)
                        {
                            throw new RankException("objects are not aligned");
                        }
                        if (result.Shape[0] != a.Shape[0])
                        {
                            throw new RankException("objects are not aligned");
                        }
                        // TODO: check strides
                    }
                    if ((a.Flags & Flags.Transposed) != 0)
                    {
                        transA = !transA;
                        a      = a.T;
                    }
                    // y:= A*x + beta*y
                    if (a.Stride[1] == 1)
                    {
                        Blas.gemv(Order.RowMajor, transA ? Transpose.Trans : Transpose.NoTrans, a.Shape[0], a.Shape[1], alpha,
                                  a.Values, a.Offset, a.Stride[0],
                                  b.Values, b.Offset, b.Stride[0],
                                  beta,
                                  result.Values, result.Offset, result.Stride[0]);
                    }
                    else
                    {
                        // y *= beta
                        if (beta != 1)
                        {
                            result.Scale(beta, result: result);
                        }

                        int offB = b.Offset;
                        int offA = a.Offset;
                        for (int j = 0; j < b.Shape[0]; ++j)
                        {
                            Blas.axpy(a.Shape[0], alpha * b.Values[offB],
                                      a.Values, offA, a.Stride[0],
                                      result.Values, result.Offset, result.Stride[0]);
                            offB += b.Stride[0];
                            offA += a.Stride[1];
                        }
                    }

                    return(result);
                }
                else if (a.Shape.Length == 3)    // tensor x vector = mat
                {
                    if (a.Shape[2] != b.Shape[0])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(a.Shape[0], a.Shape[1]);
                    }
                    else if (result.Shape[0] != a.Shape[0] || result.Shape[1] != a.Shape[1])
                    {
                        throw new RankException("objects are not aligned");
                    }

                    var offsetk   = a.Offset;
                    var offsetRes = result.Offset;

                    for (var k = 0; k < result.Shape[0]; k++)
                    {
                        var offsetj = offsetk;
                        for (var j = 0; j < result.Shape[1]; j++)
                        {
                            result.Values[offsetRes] = alpha * Blas.dot(a.Shape[2], a.Values, offsetj, a.Stride[2], b.Values, b.Offset, b.Stride[0]);
                            offsetj   += a.Stride[1];
                            offsetRes += result.Stride[1];
                        }
                        offsetk += a.Stride[0];
                    }
                    return(result);
                }
                throw new NotImplementedException();
            }
            else if (a.Shape.Length == 2 && b.Shape.Length == 2)    // matrix x matrix
            {
                if (a.Shape[1] != b.Shape[0])
                {
                    throw AssertArray.BadRank("objects are not aligned: [{0}, {1}] dot [{2}, {3}]", a.Shape[0], a.Shape[1], b.Shape[0], b.Shape[1]);
                }
                if (result == null)
                {
                    result = new Array <Real>(a.Shape[0], b.Shape[1]);
                }
                else
                {
                    if (result.Shape[0] != a.Shape[0] || result.Shape[1] != b.Shape[1])
                    {
                        throw AssertArray.BadRank("result target have incorrect shape: [{0}, {1}] instead of [{2}, {3}].", result.Shape[0], result.Shape[1], a.Shape[0], b.Shape[1]);
                    }
                    // TODO: check strides
                }
                var m = a.Shape[0];
                var n = b.Shape[1];
                var k = a.Shape[1];
                if ((a.Flags & Flags.Transposed) != 0)
                {
                    transA = !transA;
                    a      = a.T;
                }
                if ((b.Flags & Flags.Transposed) != 0)
                {
                    transB = !transB;
                    b      = b.T;
                }
                // C:= alpha * op(A) * op(B) + beta * C
                Blas.gemm(Order.RowMajor, transA ? Transpose.Trans : Transpose.NoTrans, transB ? Transpose.Trans : Transpose.NoTrans,
                          m, n, k,
                          alpha,
                          a.Values, a.Offset, a.Stride[0],
                          b.Values, b.Offset, b.Stride[0],
                          beta,
                          result.Values, result.Offset, result.Stride[0]);
                return(result);
            }
            // tensor x tensor
            throw new NotImplementedException();
        }