EDN Admin
Well-known member
Hi,
I build a function that calculates the determinant, yet it gives a stack overflow exception. I tried hard to figure out what is the error but fails to identify anything wrong. Everything seems to me to be very consistent but not for the compiler. Here is the code that tests the function.Option Strict On
Imports System.Math
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mat(,) As Double = {{1, 1, 0}, {2, -1, 1}, {3, 3, -2}}
Dim D As Double = Det(Mat)
Me.Text = CStr(D)
End Sub
Public Function Det(ByRef Matrix(,) As Double) As Double
If Not Sqrt(Matrix.Length) = CInt(Sqrt(Matrix.Length)) Then
Throw New Exception("Not a square matrix")
End If
Dim Size As Integer = CInt(Sqrt(Matrix.Length))
If Size = 1 Then Return Matrix(0, 0)
Dim Result As Double = 0
Dim SubMatrix(Size - 1, Size - 1) As Double This line generates error
For i As Integer = 0 To Size - 1
For j As Integer = 0 To i - 1
For k As Integer = 0 To Size - 2
SubMatrix(k, j) = Matrix(k + 1, j)
Next
Next
For j = i + 1 To Size - 1
For k As Integer = 0 To Size - 2
SubMatrix(k, j - 1) = Matrix(k + 1, j)
Next
Next
Result += (-1) ^ i * Matrix(0, i) * Det(SubMatrix)
Next
Return Result
End Function
End Class
It interesting to note that when I tried to write the completely equivalent code in C#, it did not give any exceptions which baffled me much more. Here is the equivalent code in C# that does not give any exceptions:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double[,] mat = { { 1, 1, 0 }, { 2, -1, 1 }, { 3, 3, -2 } };
double d = det(mat);
this.Text = d.ToString();
}
//Determinant
double det(double[,] matrix)
{
if (Math.Sqrt(matrix.Length) != (int)Math.Sqrt(matrix.Length))
throw new Exception("Not square matrix");
int size = (int)Math.Sqrt(matrix.Length);
if (size == 1) return matrix[0, 0];
double result = 0;
double[,] submatrix = new double[size - 1, size - 1];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < i; j++)
{
for (int k = 0; k < size - 1; k++)
{
submatrix[k, j] = matrix[k + 1, j];
}
}
for (int j = i + 1; j < size; j++)
{
for (int k = 0; k < size - 1; k++)
{
submatrix[k, j - 1] = matrix[k + 1, j];
}
}
result += Math.Pow(-1, i) * matrix[0, i] * det(submatrix);
}
return result;
}
}
}
Thanks to any help.
View the full article
I build a function that calculates the determinant, yet it gives a stack overflow exception. I tried hard to figure out what is the error but fails to identify anything wrong. Everything seems to me to be very consistent but not for the compiler. Here is the code that tests the function.Option Strict On
Imports System.Math
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mat(,) As Double = {{1, 1, 0}, {2, -1, 1}, {3, 3, -2}}
Dim D As Double = Det(Mat)
Me.Text = CStr(D)
End Sub
Public Function Det(ByRef Matrix(,) As Double) As Double
If Not Sqrt(Matrix.Length) = CInt(Sqrt(Matrix.Length)) Then
Throw New Exception("Not a square matrix")
End If
Dim Size As Integer = CInt(Sqrt(Matrix.Length))
If Size = 1 Then Return Matrix(0, 0)
Dim Result As Double = 0
Dim SubMatrix(Size - 1, Size - 1) As Double This line generates error
For i As Integer = 0 To Size - 1
For j As Integer = 0 To i - 1
For k As Integer = 0 To Size - 2
SubMatrix(k, j) = Matrix(k + 1, j)
Next
Next
For j = i + 1 To Size - 1
For k As Integer = 0 To Size - 2
SubMatrix(k, j - 1) = Matrix(k + 1, j)
Next
Next
Result += (-1) ^ i * Matrix(0, i) * Det(SubMatrix)
Next
Return Result
End Function
End Class
It interesting to note that when I tried to write the completely equivalent code in C#, it did not give any exceptions which baffled me much more. Here is the equivalent code in C# that does not give any exceptions:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double[,] mat = { { 1, 1, 0 }, { 2, -1, 1 }, { 3, 3, -2 } };
double d = det(mat);
this.Text = d.ToString();
}
//Determinant
double det(double[,] matrix)
{
if (Math.Sqrt(matrix.Length) != (int)Math.Sqrt(matrix.Length))
throw new Exception("Not square matrix");
int size = (int)Math.Sqrt(matrix.Length);
if (size == 1) return matrix[0, 0];
double result = 0;
double[,] submatrix = new double[size - 1, size - 1];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < i; j++)
{
for (int k = 0; k < size - 1; k++)
{
submatrix[k, j] = matrix[k + 1, j];
}
}
for (int j = i + 1; j < size; j++)
{
for (int k = 0; k < size - 1; k++)
{
submatrix[k, j - 1] = matrix[k + 1, j];
}
}
result += Math.Pow(-1, i) * matrix[0, i] * det(submatrix);
}
return result;
}
}
}
Thanks to any help.
View the full article