completed UpdateWithGoodIds
This commit is contained in:
parent
b991153b8b
commit
d337fba6fd
@ -12,18 +12,33 @@ public class Matrix2 {
|
||||
}
|
||||
|
||||
public static Matrix2 Identity(int size) {
|
||||
float[,] I = new float[size, size];
|
||||
for (int i = 0; i < size; i++) I[i, i] = 1.0f;
|
||||
return new Matrix2(I);
|
||||
return Diagonal(1, size);
|
||||
// float[,] resultData = new float[size, size];
|
||||
// for (int i = 0; i < size; i++)
|
||||
// resultData[i, i] = 1.0f;
|
||||
// return new Matrix2(resultData);
|
||||
}
|
||||
|
||||
public static Matrix2 Diagonal(Matrix1 v) {
|
||||
float[,] resultData = new float[v.magnitude, v.magnitude];
|
||||
for (int ix = 0; ix < v.magnitude; ix++)
|
||||
resultData[ix, ix] = v.data[ix];
|
||||
return new Matrix2(resultData);
|
||||
}
|
||||
public static Matrix2 Diagonal(float f, int size) {
|
||||
float[,] resultData = new float[size, size];
|
||||
for (int ix = 0; ix < size; ix++)
|
||||
resultData[ix, ix] = f;
|
||||
return new Matrix2(resultData);
|
||||
}
|
||||
|
||||
public Matrix2 Transpose() {
|
||||
float[,] r = new float[this.nCols, this.nRows];
|
||||
float[,] resultData = new float[this.nCols, this.nRows];
|
||||
for (uint rowIx = 0; rowIx < this.nRows; rowIx++) {
|
||||
for (uint colIx = 0; colIx < this.nCols; colIx++)
|
||||
r[colIx, rowIx] = this.data[rowIx, colIx];
|
||||
resultData[colIx, rowIx] = this.data[rowIx, colIx];
|
||||
}
|
||||
return new Matrix2(r);
|
||||
return new Matrix2(resultData);
|
||||
// double checked code
|
||||
}
|
||||
|
||||
@ -90,36 +105,62 @@ public class Matrix2 {
|
||||
return A * scalar;
|
||||
}
|
||||
|
||||
public Matrix2 Slice(int from, int to) {
|
||||
if (from < 0 || to >= this.nRows)
|
||||
throw new System.ArgumentException("Slice index out of range.");
|
||||
|
||||
float[,] result = new float[to - from, this.nCols];
|
||||
int resultRowIx = 0;
|
||||
for (int rowIx = from; rowIx < to; rowIx++) {
|
||||
for (int colIx = 0; colIx < this.nCols; colIx++) {
|
||||
result[resultRowIx, colIx] = this.data[rowIx, colIx];
|
||||
}
|
||||
resultRowIx++;
|
||||
}
|
||||
|
||||
return new Matrix2(result);
|
||||
}
|
||||
}
|
||||
|
||||
public class Matrix1 {
|
||||
public float[] data { get; }
|
||||
|
||||
public int size => data.GetLength(0);
|
||||
public int magnitude => data.GetLength(0);
|
||||
|
||||
public Matrix1(float[] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Matrix2 Transpose() {
|
||||
float[,] r = new float[1, this.size];
|
||||
for (uint colIx = 0; colIx < this.size; colIx++)
|
||||
float[,] r = new float[1, this.magnitude];
|
||||
for (uint colIx = 0; colIx < this.magnitude; colIx++)
|
||||
r[1, colIx] = this.data[colIx];
|
||||
|
||||
return new Matrix2(r);
|
||||
}
|
||||
|
||||
public static float Dot(Matrix1 a, Matrix1 b) {
|
||||
if (a.size != b.size)
|
||||
if (a.magnitude != b.magnitude)
|
||||
throw new System.ArgumentException("Vectors must be of the same length.");
|
||||
|
||||
float result = 0.0f;
|
||||
for (int i = 0; i < a.size; i++) {
|
||||
for (int i = 0; i < a.magnitude; i++) {
|
||||
result += a.data[i] * b.data[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Matrix1 Slice(int from, int to) {
|
||||
if (from < 0 || to >= this.magnitude)
|
||||
throw new System.ArgumentException("Slice index out of range.");
|
||||
|
||||
float[] result = new float[to - from];
|
||||
int resultIx = 0;
|
||||
for (int ix = from; ix < to; ix++)
|
||||
result[resultIx++] = this.data[ix];
|
||||
|
||||
return new Matrix1(result);
|
||||
}
|
||||
}
|
||||
|
||||
public class Matrix {
|
||||
|
Loading…
x
Reference in New Issue
Block a user