mix two two-dimensional arrays into 1 two-dimensional array - c#

i am trying the results which stored int previous arrays (1 and 2) in a new array 3 can show each result from array 1 with with each array 2, when i show the results from each one separately they are okay but when i try to mix them in array 3 i get the correct answers for the first value in array 1 with all values in array 2 then a lot of zeros appears.
here is the code for the array 3
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
and here is the whole code
double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
object[,] result3 = new object[result1.GetLength(0) * result1.GetLength(1), result2.GetLength(0) * result2.GetLength(1)];
int counter = 0;
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
}

For each m you reset i to 0 and start writing at the beginning of your array again.
You need to keep moving the index forward when you increment m.
The same is when you are combining the two indexes. When you are combining two iterations to get an index you generally multiply the first by the length of the second, then add them together.
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
int iOffset = i * result1.GetLength(1);
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
int kOffset = k * result2.GetLength(1);
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[iOffset + j, kOffset + m]);
counter++;
}
}
}
}

Related

C# [i, j] add the min "j" values ​of matrix to the new array?

[i, j] add the biggest values ​​of matrix to j the new array
its here:
But smallest not working..
Min values in "j" (not working)
for (int i = 0; i < olay; i++)
{
for (int j = 0; j < state; j++)
{
if (minimax[i] > matris[i, j])
{
minimax[i] = matris[i, j];
}
}
}
Console.WriteLine();
for (int i = 0; i < minimax.Length; i++)
{
Console.WriteLine(i + 1 + ". MINIMAX " + minimax[i]);
}
Console.ReadLine();
}
Max VALUES IN "J" (WORKING)
for (int i = 0; i < olay; i++)
{
for (int j = 0; j < state; j++)
{
if (maximax[i] < matris[i, j])
{
maximax[i] = matris[i, j];
}
}
}
/* Olaylar icin en yuksek State degerleri */
Console.WriteLine();
for (int i = 0; i < maximax.Length; i++)
{
Console.WriteLine(i + 1 + ". MAXIMAX " + maximax[i]);
}
Output console:
IMG Lınk: https://pasteboard.co/IKKQlet.jpg
The default values in your arrays are 0.
To calculate the minimum value in each column and write it to an array you can do something like this:
var resultArray = new int[rowLength];
var matrix = new int[rowLength, columnLength];
for (int i = 0; i < rowLength; i++)
{
// you have to set the first value as a minimum and after that compare
resultArray[i] = matrix[i, 0];
for (int j = 1; j < columnLength; j++)
{
if (matrix[i, j] < resultArray[i])
{
resultArray[i] = matrix[i, j];
}
}
}

How should weights be optimized with gradient descent algorithm in order to work?

I have a neural network in visual studio. for the loss function I am using a basic cost function (pred-target)**2 and after I finish an epoch I optimize the parameter functions afterwards, but the algorithm doesn't work.
No matter what is my network configuration, the predictions are not write (it is the same output for all the inputs) and the loss function is not optimized. It stays the same through all the epochs.
void calc_lyr(int x, int y, int idx, float target) // thus function calculates the neuron value based on the previous layer
{
if (x == -1 || y == 0) // if its the first layer, get the data from input nodes
{
for (int i = 0; i < neurons[y]; i++)
{
float sum = 0;
for (int j = 0; j < inputTypes.Count; j++)
{
sum += weights[x+1][j][i] * training_test[idx][j];
}
sum = relu(sum);
vals[y+1][i] = sum;
}
}
else
{
for(int i = 0; i < neurons[y]; i++)
{
float sum = 0;
for(int j = 0; j < neurons[x]; j++)
{
sum += weights[x+1][j][i] * vals[x+1][j] + biases[y][i];
}
sum = relu(sum);
vals[y+1][i] = sum;
}
}
}
void train()
{
log("Proces de antrenare inceput ----------------- " + DateTime.Now.ToString());
vals = new List<List<float>>();
weights = new List<List<List<float>>>();
biases = new List<List<float>>();
Random randB = new Random(DateTime.Now.Millisecond);
Random randW = new Random(DateTime.Now.Millisecond);
for (int i = 0; i <= nrLayers; i++)
{
progressEpochs.Value =(int)(((float)i * (float)nrLayers) / 100.0f);
vals.Add(new List<float>());
weights.Add(new List<List<float>>());
if (i == 0)
{
for (int j = 0; j < inputTypes.Count; j++)
{
vals[i].Add(0);
}
}
else
{
biases.Add(new List<float>());
for (int j = 0; j < neurons[i-1]; j++)
{
vals[i].Add(0);
float valB = (float)randB.NextDouble();
biases[i-1].Add(valB - ((int)valB));
}
}
}
float valLB = (float)randB.NextDouble();
biases.Add(new List<float>());
biases[nrLayers].Add(valLB - ((int)valLB));
for (int i = 0; i <= nrLayers; i++)
{
if (i == 0)
{
for (int j = 0; j < inputTypes.Count; j++)
{
weights[i].Add(new List<float>());
for (int x = 0; x < neurons[i]; x++)
{
float valW = (float)randW.NextDouble();
weights[i][j].Add(valW);
}
}
}
else if (i == nrLayers)
{
for (int j = 0; j < neurons[i-1]; j++) {
weights[i].Add(new List<float>());
weights[i][j].Add(0);
}
}
else
{
for (int j = 0; j < neurons[i - 1]; j++)
{
weights[i].Add(new List<float>());
for (int x = 0; x < neurons[i]; x++)
{
float valW = (float)randW.NextDouble();
weights[i][j].Add(valW);
}
}
}
}
Random rand = new Random(DateTime.Now.Millisecond);
log("\n\n");
for (int i = 0; i < epochs; i++)
{
log("Epoch " + (i + 1).ToString() + " inceput ---> " + DateTime.Now.ToString());
int idx = rand.Next() % training_test.Count;
float target = outputsPossible.IndexOf(training_labels[idx]);
for (int j = 0; j < nrLayers; j++)
{
calc_lyr(j - 1, j, idx, target);
}
float total_val = 0;
for(int x = 0; x < neurons[nrLayers - 1]; x++)
{
float val = relu(weights[nrLayers][x][0] * vals[nrLayers][x] + biases[nrLayers][0]);
total_val += val;
}
total_val = sigmoid(total_val);
float cost_res = cost(total_val, target);
log("Epoch " + (i+1).ToString() + " terminat ----- " + DateTime.Now.ToString() + "\n");
log("Eroare epoch ---> " + (cost_res<1?"0":"") + cost_res.ToString("##.##") + "\n\n\n");
float cost_der = cost_d(total_val, target);
for (int a = 0; a < weights.Count; a++)
{
for (int b = 0; b < weights[a].Count; b++)
{
for (int c = 0; c < weights[a][b].Count; c++)
{
weights[a][b][c]-=cost_der*learning_rate * sigmoid_d(weights[a][b][c]);
}
}
}
for (int a = 0; a < nrLayers; a++)
{
for (int b = 0; b < neurons[a]; b++)
{
biases[a][b] -= cost_der * learning_rate;
}
}
}
hasTrained = true;
testBut.Enabled = hasTrained;
MessageBox.Show("Antrenament complet!");
SavePrompt sp = new SavePrompt();
sp.Show();
}
How can it be changed to optimize the weights, biases and loss function? For now, when I try to debug, the weights are changing, but it is the same value for the loss function.
I solved it by using AForge.NET: link

Minimum value of maximum values of two dimensional Array

okay i have another question because as i told you i am trying to put all these values in result 3 as i mentioned before like this ( x , y ) then i have to get the max value of each point of this ( x , y ) for the whole points then get the min value of the maximum values , i managed to get the maximum values but when i try to get the minimum of them it turns shows zero.
This is the code.
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
if (result1[i, j] > result2[k, m])
{
highestMoment[i, j] = result1[i, j];
}
else
{
highestMoment[i, j] = result2[k, m];
}
Console.WriteLine(highestMoment[i, j]);
if (lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
and this the whole code
double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
object[,] result3 = new object[result1.GetLength(0), result1.GetLength(1)];
double[,] highestMoment = new double[result3.GetLength(0), result3.GetLength(1)];
double[,] lowestMoment = new double[highestMoment.GetLength(0), highestMoment.GetLength(1)];
int counter = 0;
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
//Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i +offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
//Console.WriteLine(result2[i, j]);
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
if (result1[i, j] > result2[k, m])
{
highestMoment[i, j] = result1[i, j];
}
else
{
highestMoment[i, j] = result2[k, m];
}
Console.WriteLine(highestMoment[i, j]);
if (lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
You are never assigning lowestMoment before comparing it to highestMoment. Since the default value of a double is 0 then lowestMoment will always be lower than anything you compare it to giving your results of 0.
This might be what you're looking for:
for (int i = 0; i < result1.GetLength(0); i++)
{
int iOffset = i * result1.GetLength(1);
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
int kOffset = k * result2.GetLength(1);
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[iOffset + j, kOffset + m]);
if (result1[i, j] > result2[k, m])
{
highestMoment[i, j] = result1[i, j];
}
else
{
highestMoment[i, j] = result2[k, m];
}
if (lowestMoment[i, j] == 0
|| lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(highestMoment[i, j]);
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}

Harris Corner Detection Highlighting all edges not corners

I need to implement Harris corner detection, which should only have positive R values. If I use threshold of R>0, it highlights nothing, if R < 0 however, from even -0.00001 to -1000000 it outputs perfect edge detection, but no corners.
Input and output:
http://imgur.com/a/TtQSD\
Main Code:
Bitmap b = (Bitmap)pictureBox1.Image;
var i = IntensityMat(b);
var n = Mask(i,-0.00000001);
var ty = ToBit(n);
pictureBox1.Image = ty;
Functions:
public double Intensity(Color c)
{
int x = c.R;
int y = c.G;
int z = c.B;
return Math.Sqrt(x * x + y * y + z * z);
}
public double Trace(double[,] m)
{
double sum = 0;
for (int i = 0; i < m.GetLength(0); i++)
{
sum += m[i, i];
}
return sum;
}
public double[,] M(double ix,double iy)
{
double[,] m = new double[2, 2];
m[0, 0] = ix*ix;
m[1, 1] = iy*iy;
m[0, 1] = ix * iy;
m[1, 0] = ix * iy;
return m;
}
public double Det(double[,] m)
{
return m[1, 1] * m[0, 0] - m[0, 1] * m[1, 0];
}
public double R(double[,] m,double k=0.04)
{
var t = Trace(m);
return Det(m) - k * t * t;
}
int[,] IntensityMat(Bitmap b)
{
int[,] n = new int[b.Width, b.Height];
for (int i = 0; i < b.Width; i++)
{
for (int j = 0; j < b.Height; j++)
{
Color c = b.GetPixel(i, j);
n[i, j] = (int)Intensity(c);
}
}
return n;
}
Bitmap ToBit(int[,] bd)
{
Bitmap b = new Bitmap(bd.GetLength(0), bd.GetLength(1));
for (int i = 0; i < b.Width; i++)
{
for (int j = 0; j < b.Height; j++)
{
var t = bd[i, j];
b.SetPixel(i, j, Color.FromArgb(t, t, t));
}
}
return b;
}
int[,] Mask(int[,] m,double thresh)// m matrix of I
{
int[,] n = new int[m.GetLength(0), m.GetLength(1)];
for (int i = 1; i < m.GetLength(0); i++)
{
for (int j = 1; j < m.GetLength(1); j++)
{
double ix = Math.Abs(m[i-1,j]-m[i,j]);
double iy = Math.Abs(m[i , j-1] - m[i, j]);
var lap = M(ix, iy);
var r = R(lap);
if (r > thresh)
{
n[i, j] = 255;
}
}
}
return n;
}

Unstable calculation error

I need to calculate matrix: ( X^(T) * X )^(-1).
Legend for the code&comments:
x is double[,] array;
xT - transposed matrix
^(-1) - inverted matrix
Every time i generate new random matrix to work with it and i found out that program is very unstable, because it isn't working properly with any input data. I'm sure about that because i need to get Identity matrix in the end if everything's fine, but sometimes i get a totally terrible Ineverted matrix so i don't get an Identity matrix. I'm dissappointes because i always use the same type of data and do not convert anything. Compiler is MVS 2010. Hope You will help me.
Here is my Program.cs:
static void Main(string[] args)
{
Matrix x = new Matrix(5, 4);
//Matrix temp = new Matrix(x.Row, x.Col);
//double[] y = new double[x.Row];
//double[] b = new double[x.Row];
//this data isn't calculated correctly. used for debugging
x.MatrixX[0, 0] = 7; x.MatrixX[0, 1] = 6; x.MatrixX[0, 2] = 5; x.MatrixX[0, 3] = 8;
x.MatrixX[1, 0] = 7; x.MatrixX[1, 1] = 5; x.MatrixX[1, 2] = 8; x.MatrixX[1, 3] = 5;
x.MatrixX[2, 0] = 6; x.MatrixX[2, 1] = 8; x.MatrixX[2, 2] = 6; x.MatrixX[2, 3] = 8;
x.MatrixX[3, 0] = 8; x.MatrixX[3, 1] = 5; x.MatrixX[3, 2] = 8; x.MatrixX[3, 3] = 7;
x.MatrixX[4, 0] = 8; x.MatrixX[4, 1] = 5; x.MatrixX[4, 2] = 6; x.MatrixX[4, 3] = 7;
/*
7,00000 6,00000 5,00000 8,00000
7,00000 5,00000 8,00000 5,00000
6,00000 8,00000 6,00000 8,00000
8,00000 5,00000 8,00000 7,00000
8,00000 5,00000 6,00000 7,00000
*/
//random matrix generation
/*
Random rnd = new Random();
for (int i = 0; i < x.Row; i++)
for (int j = 0; j < x.Col; j++)
x.MatrixX[i, j] = rnd.Next(5, 10);
*/
/*i'm going to calculate: ( X^(T) * X )^(-1)
* 1. transpose X
* 2. multiply X and (1)
* 3. invert matrix (2)
* +4. i wanna check the results: Multilate of (2) and (3) = Identity_matrix.
* */
Matrix.Display(x);
//1
Matrix xt = Matrix.Transpose(x);
Matrix.Display(xt);
//2
Matrix xxt = Matrix.Multiply(x, xt);
Matrix.Display(xxt);
//3
Matrix xxtinv = Matrix.Invert(Matrix.Multiply(x, xt));
Matrix.Display(xxtinv);
//4
Console.WriteLine("Invert(xxt) * xxt. IdentityMatrix:");
Matrix IdentityMatrix = Matrix.Multiply(xxtinv, xxt);
Matrix.Display(IdentityMatrix);
Console.ReadKey();
}
And here is Matrix.cs with all functions:
public class Matrix
{
private double[,] matrix;
private int row;
private int col;
#region constructors
public Matrix(int Row, int Col)
{
this.row = Row;
this.col = Col;
matrix = new double[Row, Col];
}
public Matrix()
{
Random rnd = new Random();
Row = rnd.Next(3, 7);
Col = rnd.Next(3, 7);
matrix = new double[Row, Col];
for (int i = 0; i < Row; i++)
for (int j = 0; j < Col; j++)
matrix[i, j] = rnd.Next(5, 10);
}
public Matrix(Matrix a)
{
this.Col = a.Col;
this.Row = a.Row;
this.matrix = a.matrix;
}
#endregion
#region properties
public int Col
{
get { return col; }
set { col = value; }
}
public int Row
{
get { return row; }
set { row = value; }
}
public double[,] MatrixX
{
get { return matrix; }
set { matrix = value; }
}
#endregion
static public Matrix Transpose(Matrix array)
{
Matrix temp = new Matrix(array.Col, array.Row);
for (int i = 0; i < array.Row; i++)
for (int j = 0; j < array.Col; j++)
temp.matrix[j, i] = array.matrix[i, j];
return temp;
}
static public void Display(Matrix array)
{
for (int i = 0; i < array.Row; i++)
{
for (int j = 0; j < array.Col; j++)
Console.Write("{0,5:f2}\t", array.matrix[i, j]);
Console.WriteLine();
}
Console.WriteLine();
}
static public Matrix Multiply(Matrix a, Matrix b)
{
if (a.Col != b.Row) throw new Exception("multiplication is impossible: a.Col != b.Row");
Matrix r = new Matrix(a.Row, b.Col);
for (int i = 0; i < a.Row; i++)
{
for (int j = 0; j < b.Col; j++)
{
double sum = 0;
for (int k = 0; k < b.Row; k++)
sum += a.matrix[i, k] * b.matrix[k, j];
r.matrix[i, j] = sum;
}
}
return r;
}
static public Matrix Invert(Matrix a)
{
Matrix E = new Matrix(a.Row, a.Col);
double temp = 0;
int n = a.Row;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
E.matrix[i, j] = 0.0;
if (i == j)
E.matrix[i, j] = 1.0;
}
for (int k = 0; k < n; k++)
{
temp = a.matrix[k, k];
for (int j = 0; j < n; j++)
{
a.matrix[k, j] /= temp;
E.matrix[k, j] /= temp;
}
for (int i = k + 1; i < n; i++)
{
temp = a.matrix[i, k];
for (int j = 0; j < n; j++)
{
a.matrix[i, j] -= a.matrix[k, j] * temp;
E.matrix[i, j] -= E.matrix[k, j] * temp;
}
}
}
for (int k = n - 1; k > 0; k--)
{
for (int i = k - 1; i >= 0; i--)
{
temp = a.matrix[i, k];
for (int j = 0; j < n; j++)
{
a.matrix[i, j] -= a.matrix[k, j] * temp;
E.matrix[i, j] -= E.matrix[k, j] * temp;
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
a.matrix[i, j] = E.matrix[i, j];
}
return a;
}
}
In your example, the determinant of x * transpose(x) is zero. As a result, there is no inverse, which is probably why you're getting strange results.
I also note that your Inverse function modifies the matrix passed to it. This should probably be modified to avoid that.

Categories

Resources