Example #1
0
        private ComputationResult Sum(ComputationResult computationResult)
        {
            if (computationResult.IsVectorResult)
            {
                return(new ComputationResult(VectorHelper.Sum(computationResult.VectorData.GetFlatData(), 0, computationResult.VectorData.GetFlatData().Length)));
            }
            else if (computationResult.IsOdResult)
            {
                float total      = 0.0f;
                var   data       = computationResult.OdData.GetFlatData();
                var   syncTarget = new object();

                System.Threading.Tasks.Parallel.For(0, data.Length,
                                                    () => 0.0f,
                                                    (int i, ParallelLoopState _, float localSum) =>
                {
                    return(VectorHelper.Sum(data[i], 0, data[i].Length) + localSum);
                },
                                                    (float localTotal) =>
                {
                    lock (syncTarget)
                    {
                        total += localTotal;
                    }
                });
                return(new ComputationResult(total));
            }
            return(new ComputationResult("Unknown data type to sum!"));
        }
Example #2
0
 public ComputationResult(ComputationResult res, VectorDirection direction)
 {
     OdData       = res.OdData;
     LiteralValue = res.LiteralValue;
     VectorData   = res.VectorData;
     Direction    = direction;
 }
Example #3
0
        private ComputationResult TransposeOd(ComputationResult computationResult)
        {
            var ret        = computationResult.Accumulator ? computationResult.OdData : computationResult.OdData.CreateSimilarArray <float>();
            var flatRet    = ret.GetFlatData();
            var flatOrigin = computationResult.OdData.GetFlatData();

            for (int i = 0; i < flatOrigin.Length; i++)
            {
                for (int j = i + 1; j < flatOrigin[i].Length; j++)
                {
                    var temp = flatOrigin[i][j];
                    flatRet[i][j] = flatOrigin[j][i];
                    flatRet[j][i] = temp;
                }
            }
            // if this is a new matrix copy the diagonal
            if (!computationResult.Accumulator)
            {
                for (int i = 0; i < flatRet.Length; i++)
                {
                    flatRet[i][i] = flatOrigin[i][i];
                }
            }
            return(new ComputationResult(ret, true));
        }
Example #4
0
        private ComputationResult Matrix(ComputationResult computationResult)
        {
            var vectorData = computationResult.VectorData;
            var newMatrix  = vectorData.CreateSquareTwinArray <float>();
            var flatVector = vectorData.GetFlatData();
            var flatMatrix = newMatrix.GetFlatData();

            switch (computationResult.Direction)
            {
            case ComputationResult.VectorDirection.Unassigned:
                return(new ComputationResult("Matrix was executed with an unassigned orientation vector!"));

            case ComputationResult.VectorDirection.Vertical:
                // each row is the single value
                for (int i = 0; i < flatMatrix.Length; i++)
                {
                    VectorHelper.Set(flatMatrix[i], flatVector[i]);
                }
                break;

            case ComputationResult.VectorDirection.Horizontal:
                // each column is the single value
                for (int i = 0; i < flatMatrix.Length; i++)
                {
                    Array.Copy(flatVector, flatMatrix[i], flatVector.Length);
                }
                break;
            }
            return(new ComputationResult(newMatrix, true));
        }
Example #5
0
 private ComputationResult Length(ComputationResult computationResult)
 {
     if (computationResult.IsOdResult)
     {
         return(new ComputationResult(computationResult.OdData.Count));
     }
     if (computationResult.IsVectorResult)
     {
         return(new ComputationResult(computationResult.VectorData.Count));
     }
     return(new ComputationResult("An unknown data type was processed through Length!"));
 }
Example #6
0
        private ComputationResult SumRows(ComputationResult computationResult)
        {
            var data     = computationResult.OdData;
            var ret      = new SparseArray <float>(data.Indexes);
            var flatRet  = ret.GetFlatData();
            var flatData = data.GetFlatData();

            System.Threading.Tasks.Parallel.For(0, flatData.Length, (int i) =>
            {
                flatRet[i] = VectorHelper.Sum(flatData[i], 0, flatData[i].Length);
            });
            return(new ComputationResult(ret, true, ComputationResult.VectorDirection.Vertical));
        }
Example #7
0
        private ComputationResult AvgColumns(ComputationResult computationResult)
        {
            var data     = computationResult.OdData;
            var ret      = new SparseArray <float>(data.Indexes);
            var flatRet  = ret.GetFlatData();
            var flatData = data.GetFlatData();

            System.Threading.Tasks.Parallel.For(0, flatData.Length, (int i) =>
            {
                VectorHelper.Add(flatRet, 0, flatRet, 0, flatData[i], 0, flatData[i].Length);
            });
            VectorHelper.Divide(flatRet, flatRet, flatData.Length);
            return(new ComputationResult(ret, true, ComputationResult.VectorDirection.Horizontal));
        }
Example #8
0
 private ComputationResult Evaluate(ComputationResult mulLhs, ComputationResult mulRhs, ComputationResult add)
 {
     if (add.IsValue)
     {
         return(EvaluateAddIsValue(mulLhs, mulRhs, add));
     }
     else if (add.IsVectorResult)
     {
         return(EvaluateAddIsVector(mulLhs, mulRhs, add));
     }
     else
     {
         return(EvaluateAddIsMatrix(mulLhs, mulRhs, add));
     }
 }
Example #9
0
 private ComputationResult LengthRows(ComputationResult computationResult)
 {
     if (computationResult.IsOdResult)
     {
         var data     = computationResult.OdData;
         var ret      = new SparseArray <float>(data.Indexes);
         var flatRet  = ret.GetFlatData();
         var flatData = data.GetFlatData();
         for (int i = 0; i < flatData.Length; i++)
         {
             flatRet[i] = flatData[i].Length;
         }
         return(new ComputationResult(ret, true, ComputationResult.VectorDirection.Vertical));
     }
     return(new ComputationResult("An unknown data type was processed through LengthRows!"));
 }
Example #10
0
 private ComputationResult Avg(ComputationResult computationResult)
 {
     if (computationResult.IsVectorResult)
     {
         var flat = computationResult.VectorData.GetFlatData();
         return(new ComputationResult(VectorHelper.Sum(flat, 0, flat.Length) / flat.Length));
     }
     else
     {
         var flat  = computationResult.OdData.GetFlatData();
         var total = 0.0f;
         var count = 0;
         for (int i = 0; i < flat.Length; i++)
         {
             total += VectorHelper.Sum(flat[i], 0, flat[i].Length);
             count += flat[i].Length;
         }
         return(new ComputationResult(total / count));
     }
 }
Example #11
0
 private ComputationResult Abs(ComputationResult computationResult)
 {
     if (computationResult.IsValue)
     {
         return(new ComputationResult(Math.Abs(computationResult.LiteralValue)));
     }
     else if (computationResult.IsVectorResult)
     {
         var retVector = computationResult.Accumulator ? computationResult.VectorData : computationResult.VectorData.CreateSimilarArray <float>();
         var flat      = retVector.GetFlatData();
         VectorHelper.Abs(flat, computationResult.VectorData.GetFlatData());
         return(new ComputationResult(retVector, true));
     }
     else
     {
         var retMatrix = computationResult.Accumulator ? computationResult.OdData : computationResult.OdData.CreateSimilarArray <float>();
         var flat      = retMatrix.GetFlatData();
         VectorHelper.Abs(flat, computationResult.OdData.GetFlatData());
         return(new ComputationResult(retMatrix, true));
     }
 }
Example #12
0
        private ComputationResult IdentityMatrix(ComputationResult computationResult)
        {
            SparseTwinIndex <float> ret;

            if (computationResult.IsVectorResult)
            {
                var vector = computationResult.VectorData;
                ret = vector.CreateSquareTwinArray <float>();
            }
            else
            {
                var matrix = computationResult.OdData;
                ret = matrix.CreateSimilarArray <float>();
            }
            var flatRet = ret.GetFlatData();

            for (int i = 0; i < flatRet.Length; i++)
            {
                flatRet[i][i] = 1.0f;
            }
            return(new ComputationResult(ret, true));
        }
Example #13
0
 private ComputationResult LengthColumns(ComputationResult computationResult)
 {
     if (computationResult.IsOdResult)
     {
         var data     = computationResult.OdData;
         var ret      = new SparseArray <float>(data.Indexes);
         var flatRet  = ret.GetFlatData();
         var flatData = data.GetFlatData();
         System.Threading.Tasks.Parallel.For(0, flatData.Length, (int i) =>
         {
             int temp = 0;
             for (int j = 0; j < flatData.Length; j++)
             {
                 if (flatData[j].Length > i)
                 {
                     temp++;
                 }
             }
             flatRet[i] = temp;
         });
         return(new ComputationResult(ret, true, ComputationResult.VectorDirection.Horizontal));
     }
     return(new ComputationResult("An unknown data type was processed through LengthColumns!"));
 }
Example #14
0
 private ComputationResult EvaluateAddIsVector(ComputationResult lhs, ComputationResult rhs, ComputationResult add)
 {
     // Test the simple case of this really just being an add with a constant multiply
     if (lhs.IsValue && rhs.IsValue)
     {
         var retVector = add.Accumulator ? add.VectorData : add.VectorData.CreateSimilarArray <float>();
         VectorHelper.Add(retVector.GetFlatData(), add.VectorData.GetFlatData(), lhs.LiteralValue * rhs.LiteralValue);
         return(new ComputationResult(retVector, true, add.Direction));
     }
     if (lhs.IsOdResult || rhs.IsOdResult)
     {
         if (lhs.IsVectorResult && lhs.Direction == ComputationResult.VectorDirection.Unassigned)
         {
             return(new ComputationResult("Unable to multiply vector without directionality starting at position " + MulLhs.Start + "!"));
         }
         if (rhs.IsVectorResult && lhs.Direction == ComputationResult.VectorDirection.Unassigned)
         {
             return(new ComputationResult("Unable to multiply vector without directionality starting at position " + MulRhs.Start + "!"));
         }
         if (add.Direction == ComputationResult.VectorDirection.Unassigned)
         {
             return(new ComputationResult("Unable to add vector without directionality starting at position " + Add.Start + "!"));
         }
         // if the lhs is a value just swap the two around
         if (!lhs.IsOdResult)
         {
             Swap(ref lhs, ref rhs);
         }
         //LHS is a matrix
         if (rhs.IsOdResult)
         {
             var retMatrix = rhs.Accumulator ? rhs.OdData :
                             (lhs.Accumulator ? lhs.OdData : lhs.OdData.CreateSimilarArray <float>());
             if (add.Direction == ComputationResult.VectorDirection.Vertical)
             {
                 VectorHelper.FusedMultiplyAddVerticalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.OdData.GetFlatData(), add.VectorData.GetFlatData());
             }
             else
             {
                 VectorHelper.FusedMultiplyAddHorizontalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.OdData.GetFlatData(), add.VectorData.GetFlatData());
             }
             return(new ComputationResult(retMatrix, true));
         }
         else if (rhs.IsVectorResult)
         {
             var retMatrix = lhs.Accumulator ? lhs.OdData : lhs.OdData.CreateSimilarArray <float>();
             if (rhs.Direction == ComputationResult.VectorDirection.Vertical)
             {
                 if (add.Direction == ComputationResult.VectorDirection.Vertical)
                 {
                     VectorHelper.FusedMultiplyAddVerticalRhsVerticalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.VectorData.GetFlatData(), add.VectorData.GetFlatData());
                 }
                 else
                 {
                     VectorHelper.FusedMultiplyAddVerticalRhsHorizontalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.VectorData.GetFlatData(), add.VectorData.GetFlatData());
                 }
             }
             else
             {
                 if (add.Direction == ComputationResult.VectorDirection.Vertical)
                 {
                     VectorHelper.FusedMultiplyAddHorizontalRhsVerticalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.VectorData.GetFlatData(), add.VectorData.GetFlatData());
                 }
                 else
                 {
                     VectorHelper.FusedMultiplyAddHorizontalRhsHorizontalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.VectorData.GetFlatData(), add.VectorData.GetFlatData());
                 }
             }
             return(new ComputationResult(retMatrix, true));
         }
         else
         {
             //RHS is a scalar
             var retMatrix = lhs.Accumulator ? lhs.OdData : lhs.OdData.CreateSimilarArray <float>();
             if (add.Direction == ComputationResult.VectorDirection.Vertical)
             {
                 VectorHelper.FusedMultiplyAddVerticalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.LiteralValue, add.VectorData.GetFlatData());
             }
             else
             {
                 VectorHelper.FusedMultiplyAddHorizontalAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.LiteralValue, add.VectorData.GetFlatData());
             }
             return(new ComputationResult(retMatrix, true));
         }
     }
     // vector cases
     else
     {
         // if the lhs is a value just swap the two around
         if (lhs.IsValue)
         {
             Swap(ref lhs, ref rhs);
         }
         // vector * vector + vector
         if (rhs.IsVectorResult)
         {
             var retVector = add.Accumulator ? add.VectorData :
                             (rhs.Accumulator ? rhs.VectorData :
                              (lhs.Accumulator ? lhs.VectorData : lhs.VectorData.CreateSimilarArray <float>()));
             VectorHelper.FusedMultiplyAdd(retVector.GetFlatData(), lhs.VectorData.GetFlatData(), rhs.VectorData.GetFlatData(), add.VectorData.GetFlatData());
             return(new ComputationResult(retVector, true, add.Direction == lhs.Direction && add.Direction == rhs.Direction ? add.Direction : ComputationResult.VectorDirection.Unassigned));
         }
         // vector * lit + vector
         else
         {
             var retVector = add.Accumulator ? add.VectorData :
                             (lhs.Accumulator ? lhs.VectorData : lhs.VectorData.CreateSimilarArray <float>());
             VectorHelper.FusedMultiplyAdd(retVector.GetFlatData(), lhs.VectorData.GetFlatData(), rhs.LiteralValue, add.VectorData.GetFlatData());
             return(new ComputationResult(retVector, true, add.Direction == lhs.Direction && add.Direction == rhs.Direction ? add.Direction : ComputationResult.VectorDirection.Unassigned));
         }
     }
 }
Example #15
0
 private ComputationResult EvaluateAddIsMatrix(ComputationResult lhs, ComputationResult rhs, ComputationResult add)
 {
     if (lhs.IsVectorResult && lhs.Direction == ComputationResult.VectorDirection.Unassigned)
     {
         return(new ComputationResult("Unable to multiply vector without directionality starting at position " + MulLhs.Start + "!"));
     }
     if (rhs.IsVectorResult && rhs.Direction == ComputationResult.VectorDirection.Unassigned)
     {
         return(new ComputationResult("Unable to multiply vector without directionality starting at position " + MulRhs.Start + "!"));
     }
     // Ensure that the LHS is a higher or equal order to the RHS (Matrix > Vector > Scalar)
     if (!lhs.IsOdResult)
     {
         Swap(ref lhs, ref rhs);
     }
     if (lhs.IsValue)
     {
         Swap(ref lhs, ref rhs);
     }
     // LHS is now a higher or equal to the order of RHS
     if (lhs.IsOdResult)
     {
         if (rhs.IsOdResult)
         {
             var retMatrix = add.Accumulator ? add.OdData :
                             (lhs.Accumulator ? lhs.OdData :
                              (rhs.Accumulator ? rhs.OdData : add.OdData.CreateSimilarArray <float>()));
             VectorHelper.FusedMultiplyAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.OdData.GetFlatData(), add.OdData.GetFlatData());
             return(new ComputationResult(retMatrix, true));
         }
         else if (rhs.IsVectorResult)
         {
             var retMatrix = add.Accumulator ? add.OdData :
                             (lhs.Accumulator ? lhs.OdData : add.OdData.CreateSimilarArray <float>());
             if (rhs.Direction == ComputationResult.VectorDirection.Vertical)
             {
                 VectorHelper.FusedMultiplyAddVerticalRhs(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.VectorData.GetFlatData(), add.OdData.GetFlatData());
             }
             else
             {
                 VectorHelper.FusedMultiplyAddHorizontalRhs(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.VectorData.GetFlatData(), add.OdData.GetFlatData());
             }
             return(new ComputationResult(retMatrix, true));
         }
         else
         {
             //RHS is scalar
             var retMatrix = add.Accumulator ? add.OdData :
                             (lhs.Accumulator ? lhs.OdData : add.OdData.CreateSimilarArray <float>());
             VectorHelper.FusedMultiplyAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.LiteralValue, add.OdData.GetFlatData());
             return(new ComputationResult(retMatrix, true));
         }
     }
     else if (lhs.IsVectorResult)
     {
         var retMatrix  = add.Accumulator ? add.OdData : add.OdData.CreateSimilarArray <float>();
         var tempVector = lhs.Accumulator ? lhs.VectorData : (rhs.IsVectorResult && rhs.Accumulator ? rhs.VectorData : lhs.VectorData.CreateSimilarArray <float>());
         // compute the multiplication separately in this case for better performance (n multiplies instead of n^2)
         if (rhs.IsVectorResult)
         {
             if (lhs.Direction != rhs.Direction)
             {
                 // if the directions don't add up then the sum operation would be undefined!
                 return(new ComputationResult("Unable to add vector without directionality starting at position " + MulLhs.Start + "!"));
             }
             VectorHelper.Multiply(tempVector.GetFlatData(), 0, lhs.VectorData.GetFlatData(), 0, rhs.VectorData.GetFlatData(), 0, tempVector.GetFlatData().Length);
         }
         else
         {
             VectorHelper.Multiply(tempVector.GetFlatData(), lhs.VectorData.GetFlatData(), rhs.LiteralValue);
         }
         if (lhs.Direction == ComputationResult.VectorDirection.Vertical)
         {
             VectorHelper.AddVertical(retMatrix.GetFlatData(), add.OdData.GetFlatData(), tempVector.GetFlatData());
         }
         else
         {
             VectorHelper.AddHorizontal(retMatrix.GetFlatData(), add.OdData.GetFlatData(), tempVector.GetFlatData());
         }
         return(new ComputationResult(retMatrix, true));
     }
     else
     {
         // in this case LHS is a scalar, and therefore RHS is also a scalar
         var retMatrix = add.Accumulator ? add.OdData : add.OdData.CreateSimilarArray <float>();
         VectorHelper.Add(retMatrix.GetFlatData(), add.OdData.GetFlatData(), lhs.LiteralValue * rhs.LiteralValue);
         return(new ComputationResult(retMatrix, true));
     }
 }
Example #16
0
 private ComputationResult EvaluateAddIsValue(ComputationResult lhs, ComputationResult rhs, ComputationResult add)
 {
     if (add.IsValue && lhs.IsValue && rhs.IsValue)
     {
         return(new ComputationResult(lhs.LiteralValue * rhs.LiteralValue + add.LiteralValue));
     }
     // float / matrix
     if (lhs.IsValue)
     {
         if (rhs.IsVectorResult)
         {
             var retVector = rhs.Accumulator ? rhs.VectorData : rhs.VectorData.CreateSimilarArray <float>();
             var flat      = retVector.GetFlatData();
             VectorHelper.FusedMultiplyAdd(flat, rhs.VectorData.GetFlatData(), lhs.LiteralValue, add.LiteralValue);
             return(new ComputationResult(retVector, true, rhs.Direction));
         }
         else
         {
             var retMatrix = rhs.Accumulator ? rhs.OdData : rhs.OdData.CreateSimilarArray <float>();
             // inverted lhs, rhs since order does not matter
             VectorHelper.FusedMultiplyAdd(retMatrix.GetFlatData(), rhs.OdData.GetFlatData(), lhs.LiteralValue, add.LiteralValue);
             return(new ComputationResult(retMatrix, true));
         }
     }
     else if (rhs.IsValue)
     {
         if (lhs.IsVectorResult)
         {
             var retVector = lhs.Accumulator ? lhs.VectorData : lhs.VectorData.CreateSimilarArray <float>();
             var flat      = retVector.GetFlatData();
             VectorHelper.FusedMultiplyAdd(flat, lhs.VectorData.GetFlatData(), rhs.LiteralValue, add.LiteralValue);
             return(new ComputationResult(retVector, true, lhs.Direction));
         }
         else
         {
             // matrix / float
             var retMatrix = lhs.Accumulator ? lhs.OdData : lhs.OdData.CreateSimilarArray <float>();
             VectorHelper.FusedMultiplyAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.LiteralValue, add.LiteralValue);
             return(new ComputationResult(retMatrix, true));
         }
     }
     else
     {
         if (lhs.IsVectorResult || rhs.IsVectorResult)
         {
             if (lhs.IsVectorResult && rhs.IsVectorResult)
             {
                 var retMatrix = lhs.Accumulator ? lhs.VectorData : (rhs.Accumulator ? rhs.VectorData : lhs.VectorData.CreateSimilarArray <float>());
                 VectorHelper.FusedMultiplyAdd(retMatrix.GetFlatData(), lhs.VectorData.GetFlatData(), rhs.VectorData.GetFlatData(), add.LiteralValue);
                 return(new ComputationResult(retMatrix, true, lhs.Direction));
             }
             else if (lhs.IsVectorResult)
             {
                 var retMatrix = rhs.Accumulator ? rhs.OdData : rhs.OdData.CreateSimilarArray <float>();
                 var flatRet   = retMatrix.GetFlatData();
                 var flatRhs   = rhs.OdData.GetFlatData();
                 var flatLhs   = lhs.VectorData.GetFlatData();
                 if (lhs.Direction == ComputationResult.VectorDirection.Vertical)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.FusedMultiplyAdd(flatRet[i], flatRhs[i], flatLhs[i], add.LiteralValue);
                     });
                 }
                 else if (lhs.Direction == ComputationResult.VectorDirection.Horizontal)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.FusedMultiplyAdd(flatRet[i], flatLhs, flatRhs[i], add.LiteralValue);
                     });
                 }
                 else
                 {
                     return(new ComputationResult("Unable to add vector without directionality starting at position " + MulLhs.Start + "!"));
                 }
                 return(new ComputationResult(retMatrix, true));
             }
             else
             {
                 var retMatrix = lhs.Accumulator ? lhs.OdData : lhs.OdData.CreateSimilarArray <float>();
                 var flatRet   = retMatrix.GetFlatData();
                 var flatLhs   = lhs.OdData.GetFlatData();
                 var flatRhs   = rhs.VectorData.GetFlatData();
                 if (rhs.Direction == ComputationResult.VectorDirection.Vertical)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.FusedMultiplyAdd(flatRet[i], flatLhs[i], flatRhs[i], add.LiteralValue);
                     });
                 }
                 else if (rhs.Direction == ComputationResult.VectorDirection.Horizontal)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.FusedMultiplyAdd(flatRet[i], flatRhs, flatLhs[i], add.LiteralValue);
                     });
                 }
                 else
                 {
                     return(new ComputationResult("Unable to add vector without directionality starting at position " + MulLhs.Start + "!"));
                 }
                 return(new ComputationResult(retMatrix, true));
             }
         }
         else
         {
             var retMatrix = lhs.Accumulator ? lhs.OdData : (rhs.Accumulator ? rhs.OdData : lhs.OdData.CreateSimilarArray <float>());
             VectorHelper.FusedMultiplyAdd(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.OdData.GetFlatData(), add.LiteralValue);
             return(new ComputationResult(retMatrix, true));
         }
     }
 }
Example #17
0
        public override ComputationResult Evaluate(IDataSource[] dataSources)
        {
            // first evaluate the parameters
            var values = new ComputationResult[Parameters.Length];

            for (int i = 0; i < values.Length; i++)
            {
                values[i] = Parameters[i].Evaluate(dataSources);
                if (values[i].Error)
                {
                    return(values[i]);
                }
            }

            switch (Type)
            {
            case FunctionType.AsHorizontal:
                if (values.Length != 1)
                {
                    return(new ComputationResult("AsHorizontal at position " + Start + " was executed with the wrong number of parameters!"));
                }
                if (!values[0].IsVectorResult)
                {
                    return(new ComputationResult("AsHorizontal at position " + Start + " was executed with a parameter that was not a vector!"));
                }
                return(new ComputationResult(values[0], ComputationResult.VectorDirection.Horizontal));

            case FunctionType.AsVertical:
                if (values.Length != 1)
                {
                    return(new ComputationResult("AsVertical at position " + Start + " was executed with the wrong number of parameters!"));
                }
                if (!values[0].IsVectorResult)
                {
                    return(new ComputationResult("AsVertical at position " + Start + " was executed with a parameter that was not a vector!"));
                }
                return(new ComputationResult(values[0], ComputationResult.VectorDirection.Vertical));

            case FunctionType.Transpose:
            {
                if (values.Length != 1)
                {
                    return(new ComputationResult("Transpose at position " + Start + " was executed with the wrong number of parameters!"));
                }
                if (values[0].IsVectorResult)
                {
                    switch (values[0].Direction)
                    {
                    case ComputationResult.VectorDirection.Horizontal:
                        return(new ComputationResult(values[0], ComputationResult.VectorDirection.Vertical));

                    case ComputationResult.VectorDirection.Vertical:
                        return(new ComputationResult(values[0], ComputationResult.VectorDirection.Horizontal));

                    case ComputationResult.VectorDirection.Unassigned:
                        return(new ComputationResult("Unable to transpose an vector that does not have a directionality!"));
                    }
                }
                if (values[0].IsValue)
                {
                    return(new ComputationResult("The parameter to Transpose at position " + Start + " was executed against a scalar!"));
                }
                if (values[0].IsOdResult)
                {
                    return(TransposeOd(values[0]));
                }
                return(new ComputationResult("Unsupported data type for Transpose at position " + Start + "."));
            }

            case FunctionType.SumRows:
                if (values.Length != 1)
                {
                    return(new ComputationResult("SumRows was executed with the wrong number of parameters!"));
                }
                if (!values[0].IsOdResult)
                {
                    return(new ComputationResult("SumRows was executed with a parameter that was not a matrix!"));
                }
                return(SumRows(values[0]));

            case FunctionType.SumColumns:
                if (values.Length != 1)
                {
                    return(new ComputationResult("SumColumns was executed with the wrong number of parameters!"));
                }
                if (!values[0].IsOdResult)
                {
                    return(new ComputationResult("SumColumns was executed with a parameter that was not a matrix!"));
                }
                return(SumColumns(values[0]));

            case FunctionType.AvgRows:
                if (values.Length != 1)
                {
                    return(new ComputationResult("AvgRows was executed with the wrong number of parameters!"));
                }
                if (!values[0].IsOdResult)
                {
                    return(new ComputationResult("AvgRows was executed with a parameter that was not a matrix!"));
                }
                return(AvgRows(values[0]));

            case FunctionType.AvgColumns:
                if (values.Length != 1)
                {
                    return(new ComputationResult("AvgColumns was executed with the wrong number of parameters!"));
                }
                if (!values[0].IsOdResult)
                {
                    return(new ComputationResult("AvgColumns was executed with a parameter that was not a matrix!"));
                }
                return(AvgColumns(values[0]));

            case FunctionType.Sum:
                if (values.Length != 1)
                {
                    return(new ComputationResult("Sum was executed with the wrong number of parameters!"));
                }
                if (values[0].IsValue)
                {
                    return(new ComputationResult("Sum was executed with a parameter that was already a scalar value!"));
                }
                return(Sum(values[0]));

            case FunctionType.Abs:
                if (values.Length != 1)
                {
                    return(new ComputationResult("Abs was executed with the wrong number of parameters!"));
                }
                return(Abs(values[0]));

            case FunctionType.Avg:
                if (values.Length != 1)
                {
                    return(new ComputationResult("Avg was executed with the wrong number of parameters!"));
                }
                if (values[0].IsValue)
                {
                    return(new ComputationResult("Avg was executed with a parameter that was already a scalar value!"));
                }
                return(Avg(values[0]));

            case FunctionType.E:
                return(new ComputationResult((float)Math.E));

            case FunctionType.Pi:
                return(new ComputationResult((float)Math.PI));

            case FunctionType.Length:
                if (values.Length != 1)
                {
                    return(new ComputationResult("Length was executed with the wrong number of parameters!"));
                }
                if (values[0].IsValue)
                {
                    return(new ComputationResult("Length can not be applied to a scalar!"));
                }
                return(Length(values[0]));

            case FunctionType.LengthColumns:
                if (values.Length != 1)
                {
                    return(new ComputationResult("LengthColumns was executed with the wrong number of parameters!"));
                }
                if (values[0].IsOdResult)
                {
                    return(new ComputationResult("LengthColumns must be applied to a Matrix!"));
                }
                return(LengthColumns(values[0]));

            case FunctionType.LengthRows:
                if (values.Length != 1)
                {
                    return(new ComputationResult("LengthRows was executed with the wrong number of parameters!"));
                }
                if (values[0].IsOdResult)
                {
                    return(new ComputationResult("LengthRows must be applied to a Matrix!"));
                }
                return(LengthRows(values[0]));

            case FunctionType.ZeroMatrix:
                if (values.Length != 1)
                {
                    return(new ComputationResult("ZeroMatrix was executed with the wrong number of parameters!"));
                }
                if (values[0].IsValue)
                {
                    return(new ComputationResult("ZeroMatrix must be applied to a vector, or a matrix!"));
                }
                return(ZeroMatrix(values));

            case FunctionType.Matrix:
                if (values.Length != 1)
                {
                    return(new ComputationResult("Matrix was executed with the wrong number of parameters!"));
                }
                if (!values[0].IsVectorResult)
                {
                    return(new ComputationResult("Matrix must be applied to a vector!"));
                }
                return(Matrix(values[0]));

            case FunctionType.IdentityMatrix:
                if (values.Length != 1)
                {
                    return(new ComputationResult("IdentityMatrix was executed with the wrong number of parameters!"));
                }
                if (values[0].IsValue)
                {
                    return(new ComputationResult("IdentityMatrix must be applied to a vector, or a matrix!"));
                }
                return(IdentityMatrix(values[0]));

            case FunctionType.Log:
                if (values.Length != 1)
                {
                    return(new ComputationResult("Log must be executed with one parameter!"));
                }
                return(Log(values));

            case FunctionType.If:
                if (values.Length != 3)
                {
                    return(new ComputationResult("If requires 3 parameters (condition, valueIfTrue, valueIfFalse)!"));
                }
                return(ComputeIf(values));

            case FunctionType.IfNaN:
                if (values.Length != 2)
                {
                    return(new ComputationResult("IfNaN requires 2 parameters (original,replacement)!"));
                }
                return(ComputeIfNaN(values));
            }
            return(new ComputationResult("An undefined function was executed!"));
        }
Example #18
0
 public override ComputationResult Evaluate(ComputationResult lhs, ComputationResult rhs)
 {
     // see if we have two values, in this case we can skip doing the matrix operation
     if (lhs.IsValue && rhs.IsValue)
     {
         return(new ComputationResult(lhs.LiteralValue - rhs.LiteralValue));
     }
     // float / matrix
     if (lhs.IsValue)
     {
         if (rhs.IsVectorResult)
         {
             var retVector = rhs.Accumulator ? rhs.VectorData : rhs.VectorData.CreateSimilarArray <float>();
             var flat      = retVector.GetFlatData();
             VectorHelper.Subtract(flat, lhs.VectorData.GetFlatData(), rhs.LiteralValue);
             return(new ComputationResult(retVector, true, rhs.Direction));
         }
         else
         {
             var retMatrix = rhs.Accumulator ? rhs.OdData : rhs.OdData.CreateSimilarArray <float>();
             VectorHelper.Subtract(retMatrix.GetFlatData(), lhs.LiteralValue, rhs.OdData.GetFlatData());
             return(new ComputationResult(retMatrix, true));
         }
     }
     else if (rhs.IsValue)
     {
         if (lhs.IsVectorResult)
         {
             var retVector = lhs.Accumulator ? lhs.VectorData : lhs.VectorData.CreateSimilarArray <float>();
             var flat      = retVector.GetFlatData();
             VectorHelper.Subtract(flat, lhs.VectorData.GetFlatData(), rhs.LiteralValue);
             return(new ComputationResult(retVector, true, lhs.Direction));
         }
         else
         {
             // matrix / float
             var retMatrix = lhs.Accumulator ? lhs.OdData : lhs.OdData.CreateSimilarArray <float>();
             VectorHelper.Subtract(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.LiteralValue);
             return(new ComputationResult(retMatrix, true));
         }
     }
     else
     {
         if (lhs.IsVectorResult || rhs.IsVectorResult)
         {
             if (lhs.IsVectorResult && rhs.IsVectorResult)
             {
                 var retMatrix = lhs.Accumulator ? lhs.VectorData : (rhs.Accumulator ? rhs.VectorData : lhs.VectorData.CreateSimilarArray <float>());
                 VectorHelper.Subtract(retMatrix.GetFlatData(), 0, lhs.VectorData.GetFlatData(), 0, rhs.VectorData.GetFlatData(), 0, retMatrix.GetFlatData().Length);
                 return(new ComputationResult(retMatrix, true, lhs.Direction));
             }
             else if (lhs.IsVectorResult)
             {
                 var retMatrix = rhs.Accumulator ? rhs.OdData : rhs.OdData.CreateSimilarArray <float>();
                 var flatRet   = retMatrix.GetFlatData();
                 var flatRhs   = rhs.OdData.GetFlatData();
                 var flatLhs   = lhs.VectorData.GetFlatData();
                 if (lhs.Direction == ComputationResult.VectorDirection.Vertical)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.Subtract(flatRet[i], flatLhs[i], flatRhs[i]);
                     });
                 }
                 else if (lhs.Direction == ComputationResult.VectorDirection.Horizontal)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.Subtract(flatRet[i], 0, flatLhs, 0, flatRhs[i], 0, flatRet[i].Length);
                     });
                 }
                 else
                 {
                     return(new ComputationResult("Unable to subtract vector without directionality starting at position " + Lhs.Start + "!"));
                 }
                 return(new ComputationResult(retMatrix, true));
             }
             else
             {
                 var retMatrix = lhs.Accumulator ? lhs.OdData : lhs.OdData.CreateSimilarArray <float>();
                 var flatRet   = retMatrix.GetFlatData();
                 var flatLhs   = lhs.OdData.GetFlatData();
                 var flatRhs   = rhs.VectorData.GetFlatData();
                 if (rhs.Direction == ComputationResult.VectorDirection.Vertical)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.Subtract(flatRet[i], flatLhs[i], flatRhs[i]);
                     });
                 }
                 else if (rhs.Direction == ComputationResult.VectorDirection.Horizontal)
                 {
                     System.Threading.Tasks.Parallel.For(0, flatRet.Length, i =>
                     {
                         VectorHelper.Subtract(flatRet[i], 0, flatLhs[i], 0, flatRhs, 0, flatRet[i].Length);
                     });
                 }
                 else
                 {
                     return(new ComputationResult("Unable to subtract vector without directionality starting at position " + Lhs.Start + "!"));
                 }
                 return(new ComputationResult(retMatrix, true));
             }
         }
         else
         {
             var retMatrix = lhs.Accumulator ? lhs.OdData : (rhs.Accumulator ? rhs.OdData : lhs.OdData.CreateSimilarArray <float>());
             VectorHelper.Subtract(retMatrix.GetFlatData(), lhs.OdData.GetFlatData(), rhs.OdData.GetFlatData());
             return(new ComputationResult(retMatrix, true));
         }
     }
 }
Example #19
0
 public abstract ComputationResult Evaluate(ComputationResult lhs, ComputationResult rhs);