completed UpdateWithGoodIds

This commit is contained in:
Pascal Serrarens 2025-02-12 11:57:46 +01:00
parent b991153b8b
commit d337fba6fd

View File

@ -12,18 +12,33 @@ public class Matrix2 {
} }
public static Matrix2 Identity(int size) { public static Matrix2 Identity(int size) {
float[,] I = new float[size, size]; return Diagonal(1, size);
for (int i = 0; i < size; i++) I[i, i] = 1.0f; // float[,] resultData = new float[size, size];
return new Matrix2(I); // 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() { 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 rowIx = 0; rowIx < this.nRows; rowIx++) {
for (uint colIx = 0; colIx < this.nCols; colIx++) 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 // double checked code
} }
@ -90,36 +105,62 @@ public class Matrix2 {
return A * scalar; 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 class Matrix1 {
public float[] data { get; } public float[] data { get; }
public int size => data.GetLength(0); public int magnitude => data.GetLength(0);
public Matrix1(float[] data) { public Matrix1(float[] data) {
this.data = data; this.data = data;
} }
public Matrix2 Transpose() { public Matrix2 Transpose() {
float[,] r = new float[1, this.size]; float[,] r = new float[1, this.magnitude];
for (uint colIx = 0; colIx < this.size; colIx++) for (uint colIx = 0; colIx < this.magnitude; colIx++)
r[1, colIx] = this.data[colIx]; r[1, colIx] = this.data[colIx];
return new Matrix2(r); return new Matrix2(r);
} }
public static float Dot(Matrix1 a, Matrix1 b) { 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."); throw new System.ArgumentException("Vectors must be of the same length.");
float result = 0.0f; 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]; result += a.data[i] * b.data[i];
} }
return result; 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 { public class Matrix {