Wrong Determinant Output using BasicLinearAlgebra library

The Output always Comes out to be 96

#include <BasicLinearAlgebra.h>

using namespace BLA;

void setup() {
  Serial.begin(115200);

  int j;
  Serial.println(j);
  
  char str1[] = "Hello World";
  char buffer[30];
  
  for(int i = 0; i < strlen(str1); i++){
    sprintf(buffer, "%u\n", &str1 + i);
    Serial.print(buffer);
  }

  BLA::Matrix<3,3,int> A = {9, 8, 7, 6, 5, 4, 3, 2, 1};
  BLA::Matrix<3,3,int> B = {0, 0, 1, 0, 1, 0, 1, 0, 0};
  
  Serial << "~A*B: " << ~A * B << '\n';
  
  Serial << "~A: " << ~A << '\n';

  Serial << "A: " << A << '\n';

  auto b = BLA::Determinant(A);

  Serial << "|A|: " << b << '\n';
}

void loop() {
}

Output

0
1070514050
1070514062
1070514074
1070514086
1070514098
1070514110
1070514122
1070514134
1070514146
1070514158
1070514170
A*B: [[3,6,9],[2,5,8],[1,4,7]]
~A: [[9,6,3],[8,5,2],[7,4,1]]
A: [[9,8,7],[6,5,4],[3,2,1]]
|A|: 96

The Answer should be 0, I may be using the library the wrong way but I can't find many examples so I am stuck

You're invoking Undefined Behavior. Any output of your program is meaningless anyway.
There may very well be an issue with BLA::Determinant as well, but you should fix your sketch first.

1 Like

The bug is in the sprintf line..

I let you look into it

1 Like

Even removing all possible wrong commands, the determinant A is still being printed wrong.

The determinate of the matrix A is = 0 and not 96.

Maybe an error in the library.

#include <BasicLinearAlgebra.h>
using namespace BLA;

void setup() {
  Serial.begin(115200);
 
  BLA::Matrix<3,3,int> A = {9, 8, 7, 6, 5, 4, 3, 2, 1};
  int b = BLA::Determinant(A);
  Serial << "|A|: " << b << '\n';
}

void loop() {
}

Simulate @ Matematic - Wokwi ESP32, STM32, Arduino Simulator

Determinat A
9	5	1	45	
8	4	3	96	
7	6	2	84	
				
8	6	1	-48	
9	4	2	-72	
7	5	3	-105	
				
Determinat A  =  0	

1 Like

I know that the most of my code is meaning less I was trying things out, after learning basic C/C++, and wanted to try out matrix calculations using a library that I had found, but for some reason it was showing the wrong result, that was why I asked.... sorry this may seem passive aggressive I am not trying to be.

I was trying out pointers on Arduino as I wanted to learn more about them

Should I report this on GitHub?

I suggest that you first carry out other tests and search the WEB to see if there are no errors already reported.

PS: I try Determinant B and print correct. (-1)

2 Likes

The problem is that you're violating the rules of the C++ standard, which means that the program can do anything, including (but not limited to) printing invalid results.

I'd highly recommend checking out the link I posted.

Specifically, reading the value of an uninitialized variable and using the wrong xprintf format specifier for the argument type are not allowed.


The BLA issue seems to be that the determinant implementation uses a PLU factorization. This is not a bad strategy, but it won't work for integers, because it requires dividing by the pivot.

2 Likes
#include <BasicLinearAlgebra.h>

using namespace BLA;

void setup() {
  Serial.begin(115200);
  BLA::Matrix<3,3,int> A = {9, 8, 7, 6, 5, 4, 3, 2, 1};
  Serial << "A: " << A << '\n';

  int b = BLA::Determinant(A);

  Serial << "|A|: " << b << '\n';
}

void loop() {
}

I tried this code out and it still gives the wrong result but it seems that you right!

#include <BasicLinearAlgebra.h>

using namespace BLA;

void setup() {
  Serial.begin(115200);
  BLA::Matrix<3,3> A = {9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0};
  Serial << "A: " << A << '\n';

  int b = BLA::Determinant(A);

  Serial << "|A|: " << b << '\n';
}

void loop() {
}

with this it works! answer comes out to be 0,thank you! I frankly do not know much about the LPU method you mentioned but I will look further into it! thank you!

but I still don't understand the undefined behavioral part, I did read it but I have already initialized all variables

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.