Matrix 基于 Ver 2
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Traph
{
public class Vector : ICloneable, IEquatable<Vector>, IEnumerable<double>
{
internalreadonly Matrix matrix;
public int Dimensions => matrix.Rows;
public Vector(params double[] values)
{
matrix = new Matrix(new double[values.Length, 1]);
for (var i = 0; i < values.Length; i++)
matrix[i, 0] = values[i];
}
private Vector(Matrix matrix, bool copyMatrix = false) => this.matrix = copyMatrix ? (Matrix)matrix.Clone() : matrix;
public object Clone() => new Vector((Matrix)matrix.Clone());
public bool Equals([AllowNull] Vector other) => other != null && (matrix == other.matrix);
public static bool operator ==(Vector left, Vector right) => left?.Equals(right) ?? (right == null);
public static bool operator !=(Vector left, Vector right) => !(left == right);
public IEnumerator<double> GetEnumerator() => matrix.T.First().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public override bool Equals(object obj) => Equals(obj as Vector);
public override int GetHashCode() => matrix.GetHashCode();
public override string ToString() => $"({string.Join(", ", this)})";
public Vector Add(Vector right) => new Vector(matrix + right.matrix);
public static Vector operator +(Vector left, Vector right) => left.Add(right);
public Vector Minus => new Vector(-matrix);
public static Vector operator -(Vector vector) => vector.Minus;
public static Vector operator -(Vector left, Vector right) => left.Add(right.Minus);
public double DotMultiply(Vector right)
{
if (Dimensions != right.Dimensions) throw new ArithmeticException();
double ret = 0;
var leftValues = GetEnumerator();
var rightVelues = right.GetEnumerator();
while (leftValues.MoveNext() /* && rightVelues.MoveNext() */)
{
rightVelues.MoveNext();
ret += leftValues.Current * rightVelues.Current;
}
return ret;
}
public static double operator *(Vector left, Vector right) => left.DotMultiply(right);
}
public static class VectorExtention
{
public static Vector AsVector(this double[] values) => new Vector(values);
}
}