示例#1
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static KalkMatrix Transpose(KalkMatrix m)
 {
     if (m == null)
     {
         throw new ArgumentNullException(nameof(m));
     }
     return(m.Transpose());
 }
示例#2
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 protected static void AssertSquareMatrix(KalkMatrix m)
 {
     if (m == null)
     {
         throw new ArgumentNullException(nameof(m));
     }
     if (!m.IsSquare)
     {
         throw new ArgumentException($"Matrix must be square nxn instead of {m.TypeName}", nameof(m));
     }
 }
示例#3
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static object Multiply(KalkMatrix x, KalkMatrix y)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     if (y == null)
     {
         throw new ArgumentNullException(nameof(y));
     }
     CheckElementType(x, y);
     if (x.ColumnCount != y.RowCount)
     {
         throw new ArgumentException($"Invalid size not between the matrix x {x.TypeName} with a column count {x.ColumnCount} and the matrix y {y.TypeName} with a row count of {y.RowCount}. They Must be equal.", nameof(x));
     }
     return(x.GenericMultiply(y));
 }
示例#4
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static object Multiply(KalkMatrix x, KalkVector y)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     if (y == null)
     {
         throw new ArgumentNullException(nameof(y));
     }
     CheckElementType(x, y);
     if (x.ColumnCount != y.Length)
     {
         throw new ArgumentException($"Invalid size between the vector type length {y.Length} and the matrix column count {x.ColumnCount}. They Must be equal.", nameof(x));
     }
     return(x.GenericMultiplyRight(y));
 }
示例#5
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static object Multiply(KalkVector x, KalkMatrix y)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     if (y == null)
     {
         throw new ArgumentNullException(nameof(y));
     }
     CheckElementType(x, y);
     if (x.Length != y.RowCount)
     {
         throw new ArgumentException($"Invalid size between the vector type length {x.Length} and the matrix row count {y.RowCount}. They Must be equal.", nameof(x));
     }
     return(y.GenericMultiplyLeft(x));
 }
示例#6
0
        public KalkMatrix <T> Invoke(KalkEngine context, params object[] arguments)
        {
            var matrix = new KalkMatrix <T>(RowCount, ColumnCount);

            if (arguments.Length == 0)
            {
                return(matrix);
            }

            int index       = 0;
            int columnIndex = 0;
            var maxTotal    = ColumnCount * RowCount;

            if (arguments.Length == 1)
            {
                var arg0 = arguments[0];
                if (arg0 is KalkMatrix m)
                {
                    var values = m.Values;
                    for (int j = 0; j < values.Length; j++)
                    {
                        matrix[index++] = context.ToObject <T>(0, values.GetValue(j));
                    }
                }
                else if (arg0 is IList list)
                {
                    for (int j = 0; j < list.Count; j++)
                    {
                        matrix[index++] = context.ToObject <T>(0, list[j]);
                    }
                }
                else
                {
                    var value = context.ToObject <T>(0, arg0);
                    for (int j = 0; j < ColumnCount * RowCount; j++)
                    {
                        matrix[index++] = value;
                    }
                }
            }
            else
            {
                for (var i = 0; i < arguments.Length; i++)
                {
                    var arg       = arguments[i];
                    var argLength = arg is IList list ? list.Count : 1;

                    var length = columnIndex + argLength;
                    if (length > ColumnCount)
                    {
                        throw new ScriptArgumentException(i, $"Invalid number of arguments crossing a row. Expecting {ColumnCount} arguments instead of {length} for {matrix.TypeName}.");
                    }

                    if (length == ColumnCount)
                    {
                        columnIndex = 0;
                    }
                    else
                    {
                        columnIndex += argLength;
                    }

                    if (arg is IList listToAdd)
                    {
                        for (int j = 0; j < argLength; j++)
                        {
                            matrix[index++] = context.ToObject <T>(i, listToAdd[j]);
                        }
                    }
                    else
                    {
                        var value = context.ToObject <T>(i, arg);
                        matrix[index++] = value;
                    }
                }
            }

            if (index != maxTotal)
            {
                throw new ScriptArgumentException(arguments.Length - 1, $"Invalid number of arguments. Expecting {maxTotal} arguments instead of {index} for {matrix.TypeName}.");
            }

            return(matrix);
        }
示例#7
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static KalkVector Diagonal(KalkMatrix x)
 {
     AssertSquareMatrix(x);
     return(x.Diagonal());
 }
示例#8
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static KalkMatrix Identity(KalkMatrix m)
 {
     AssertSquareMatrix(m);
     return(m.Identity());
 }
示例#9
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 protected abstract KalkMatrix GenericMultiply(KalkMatrix y);
示例#10
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static KalkMatrix Inverse(KalkMatrix m)
 {
     AssertSquareMatrix(m);
     return(m.GenericInverse());
 }
示例#11
0
文件: KalkMatrix.cs 项目: xoofx/kalk
 public static object Determinant(KalkMatrix m)
 {
     AssertSquareMatrix(m);
     return(m.GenericDeterminant());
 }