/// <summary> /// 指定されたベクトルから新しい行列を作成する. /// 指定された各ベクトルは,新しい行列において<see cref="VectorType"/>で /// 指定されたベクトルとして解釈され,コピーされる. /// </summary> /// <param name="type">指定されたベクトルの種類</param> /// <param name="vectors"></param> public Matrix(VectorType type, params IVector[] vectors) { VectorChecker.ZeroSize(vectors[0]); int size = vectors[0].Size; foreach (IVector vec in vectors) { if (size != vec.Size) { throw new Exception.MismatchSizeException(); } } int count = vectors.Length; if (type == VectorType.RowVector) { Resize(count, size); for (int r = 0; r < count; ++r) { this.Rows[r] = vectors[r]; } } else { Resize(size, count); for (int c = 0; c < count; ++c) { this.Columns[c] = vectors[c]; } } }
/// <summary> /// 要素indexの配列で指定された要素を抽出する. /// </summary> /// <param name="indexes">抽出したい要素の<c>index</c>配列</param> /// <returns>抽出した要素にアクセス可能な<see cref="IVector"/>インタフェース</returns> /// <exception cref="Exception.IllegalArgumentException"> /// 引数が不正の場合にthrowされる. /// </exception> /// <exception cref="Exception.ZeroSizeException"> /// ベクトルサイズが0の場合にthrowされる. /// </exception> public IVector this[params int[] indexes] { get { if (indexes == null) { throw new Exception.IllegalArgumentException("\"indexes\" is null."); } VectorChecker.ZeroSize(this); if (this.Size < indexes.Length) { throw new Exception.IllegalArgumentException("indexes.Length is greater than this.Size"); } foreach (int i in indexes) { if (i < 0 || this.Size <= i) { throw new Exception.IllegalArgumentException( "Index=" + i + " (which is included in the indexes) is out of range."); } } return(new SubVector(this, indexes)); } }
internal static IVector MulEq(IVector v1, double val) { VectorChecker.ZeroSize(v1); for (int i = 0; i < v1.Size; ++i) { v1[i] *= val; } return(v1); }
internal static IVector DivEq(IVector v1, double val) { VectorChecker.ValueIsLessThanLimit(val); VectorChecker.ZeroSize(v1); for (int i = 0; i < v1.Size; ++i) { v1[i] /= val; } return(v1); }
/// <summary> /// <see cref="IVector"/>の値を対角要素に持つ対角行列を新しく作成する. /// </summary> /// <param name="v">対角行列の対角要素を格納している<see cref="IVector"/></param> /// <returns>対角行列</returns> /// <exception cref="Exception.ZeroSizeException"> /// ベクトルサイズが0の場合にthrowされる. /// </exception> public static Matrix CreateDiagonalMatrix(IVector v) { VectorChecker.ZeroSize(v); Matrix m = new Matrix(v.Size, v.Size); m.Zero(); v.ForEach(delegate(int i, double e) { m[i, i] = e; }); return(m); }
/// <summary> /// /// </summary> /// <param name="cv"></param> /// <param name="rv"></param> /// <returns></returns> public static Matrix operator *(ColumnVector cv, RowVector rv) { VectorChecker.ZeroSize(cv); VectorChecker.ZeroSize(rv); Matrix ret = new Matrix(cv.Size, rv.Size); for (int r = 0; r < cv.Size; ++r) { for (int c = 0; c < rv.Size; ++c) { ret[r, c] = cv[r] * rv[c]; } } return(ret); }
private static T _Standardize_T <T>(T vector, VarianceType varType) where T : IVector { VectorChecker.ZeroSize(vector); double avg = vector.Average; double std = Math.Sqrt(varType == VarianceType.DivN ? vector.Scatter / vector.Size : vector.Variance); if (clapack.CalculationChecker.IsLessThanLimit(std)) { throw new Exception.ValueIsLessThanLimitException( "The standard variation is less than \"Calculation Limit\".\n" + "Values = " + vector.ToString()); } for (int i = 0; i < vector.Size; ++i) { vector[i] = (vector[i] - avg) / std; } return(vector); }
/// <summary> /// 指定されたベクトルから新しい行列を作成する. /// 指定された各ベクトルは,新しい行列の各列にコピーされる. /// </summary> /// <param name="arr"></param> public Matrix(params ColumnVector[] arr) { // 入力の検証 VectorChecker.ZeroSize(arr[0]); int rsize = arr[0].Size; foreach (ColumnVector cv in arr) { if (rsize != cv.Size) { throw new Exception.MismatchSizeException(); } } // 構築 int csize = arr.Length; Resize(rsize, csize); for (int c = 0; c < csize; ++c) { this.Columns[c] = arr[c]; } }
/// <summary> /// 指定されたベクトルから新しい行列を作成する. /// 指定された各ベクトルは,新しい行列の各行にコピーされる. /// </summary> /// <param name="arr"></param> public Matrix(params RowVector[] arr) { // 入力の検証 VectorChecker.ZeroSize(arr[0]); int csize = arr[0].Size; foreach (RowVector rv in arr) { if (csize != rv.Size) { throw new Exception.MismatchSizeException(); } } // 構築 int rsize = arr.Length; Resize(rsize, csize); for (int r = 0; r < rsize; ++r) { this.Rows[r] = arr[r]; } }
/// <summary> /// /// </summary> /// <param name="v"></param> /// <param name="d"></param> /// <returns></returns> public static new ColumnVector Div(IVector v, double d) { VectorChecker.ZeroSize(v); return(VectorImpl.Div(new ColumnVector(v.Size), v, d)); }
/// <summary> /// /// </summary> /// <param name="v"></param> /// <param name="d"></param> /// <returns></returns> public static new ColumnVector Mul(IVector v, double d) { VectorChecker.ZeroSize(v); return(VectorImpl.Mul(new ColumnVector(v.Size), d, v)); }
/// <summary> /// /// </summary> /// <param name="d"></param> /// <param name="v"></param> /// <returns></returns> public static ColumnVector operator *(double d, ColumnVector v) { VectorChecker.ZeroSize(v); return(VectorImpl.Mul(new ColumnVector(v.Size), d, v)); }
/// <summary> /// /// </summary> /// <param name="v"></param> /// <param name="d"></param> /// <returns></returns> public static RowVector operator /(RowVector v, double d) { VectorChecker.ZeroSize(v); return(VectorImpl.Div(new RowVector(v.Size), v, d)); }
/// <summary> /// /// </summary> /// <param name="v"></param> /// <param name="d"></param> /// <returns></returns> public static RowVector operator *(RowVector v, double d) { VectorChecker.ZeroSize(v); return(VectorImpl.Mul(new RowVector(v.Size), d, v)); }