Giter Site home page Giter Site logo

Comments (11)

DanielMartensson avatar DanielMartensson commented on May 31, 2024

Hi!

Can you post the C code and I will simulate the C code and see if a for-loop exceeds the number of array elements.

Notice that matrix A will change.

from ccontrol.

ooaj avatar ooaj commented on May 31, 2024

I used your example, i.e.,

int main() 
{
	float A[4*4] = {0.018142,   0.968856,   0.151740,   0.757174,
				    0.017829,   0.474323,   0.358832,   0.970854,
				    0.184523,   0.063063,   0.680511,   0.191901,
				    0.806877,   0.830208,   0.977169,   0.222291};

	float wr_a[4];   // Eigenvalues real
	float wi_a[4];   // Eigenvalues imaginary

	eig(A, wr_a, wi_a, 4);
	// Print eigenvalues
	printf("Real eigenvalues of A:\n");
	print(wr_a, 4, 1);
	printf("Imaginary eigenvalues of A: \n");
	print(wi_a, 4, 1);
}

Now, as for the eig.c function, in order to print the necessary values, I just modified the loop I linked in OP. So I just added

printf("k is: %d\n", k);
printf("i is: %d\n", i);

inside the loop.

I must note that the C code runs perfectly well, and finds eigenvalues just as in MATLAB. It's the array indexing I am confused by.

from ccontrol.

DanielMartensson avatar DanielMartensson commented on May 31, 2024

I used your example, i.e.,

int main() 
{
	float A[4*4] = {0.018142,   0.968856,   0.151740,   0.757174,
				    0.017829,   0.474323,   0.358832,   0.970854,
				    0.184523,   0.063063,   0.680511,   0.191901,
				    0.806877,   0.830208,   0.977169,   0.222291};

	float wr_a[4];   // Eigenvalues real
	float wi_a[4];   // Eigenvalues imaginary

	eig(A, wr_a, wi_a, 4);
	// Print eigenvalues
	printf("Real eigenvalues of A:\n");
	print(wr_a, 4, 1);
	printf("Imaginary eigenvalues of A: \n");
	print(wi_a, 4, 1);
}

Now, as for the eig.c function, in order to print the necessary values, I just modified the loop I linked in OP. So I just added

printf("k is: %d\n", k);
printf("i is: %d\n", i);

inside the loop.

I must note that the C code runs perfectly well, and finds eigenvalues just as in MATLAB. It's the array indexing I am confused by.

Yes. I currently looking at the problem. Don't worry. I will fix this.

from ccontrol.

DanielMartensson avatar DanielMartensson commented on May 31, 2024

Hi!

Can you change

if (k != (nn)) {
     printf("row*i + k+2 = %i\n", row*i + k+2);
     p += z * *(A + row*i + k+2);
     *(A + row*i + k+2) -= p * r;
}

to

if (k != (nn-1)) {
     printf("row*i + k+2 = %i\n", row*i + k+2);
     p += z * *(A + row*i + k+2);
     *(A + row*i + k+2) -= p * r;
}

And see if you can compile the C code with MATLAB?

I have made a change, see if my C code works now.

from ccontrol.

ooaj avatar ooaj commented on May 31, 2024

Hey,

Thanks for the reply! It works again perfectly on C, I will see if it works on MATLAB tomorrow morning and will share the results. Why would that fix not change the result in C though?

from ccontrol.

DanielMartensson avatar DanielMartensson commented on May 31, 2024

Hey,

Thanks for the reply! It works again perfectly on C, I will see if it works on MATLAB tomorrow morning and will share the results. Why would that fix not change the result in C though?

It was a problem in C too, but in C, the language does not warn of you exceed the limit of an array. It just happens nothing, even if the results becomes excellent.

But I have simulate it at the line you pointing to and it never exceeds the array limit now.

from ccontrol.

ooaj avatar ooaj commented on May 31, 2024

Hello again. The code now perfectly runs on MATLAB. By the way, I don't run the C code in MATLAB (like hex), just that I converted the C code to MATLAB. Thanks!

I had to fix a few other errors in my code, hence the late reply. :)

from ccontrol.

DanielMartensson avatar DanielMartensson commented on May 31, 2024

Hello again. The code now perfectly runs on MATLAB. By the way, I don't run the C code in MATLAB (like hex), just that I converted the C code to MATLAB. Thanks!

I had to fix a few other errors in my code, hence the late reply. :)

Thank you for veryfing the results =)
What tool are you using in MATLAB?

I have always wanted to convert MATLAB code to C code, but it seems that MATLAB code to C code results code with dynamical memory allocation or code that uses to much memory. That's why I'm converting the code by hand.

from ccontrol.

ooaj avatar ooaj commented on May 31, 2024

Thank you for veryfing the results =)

Thank you for CControl and EmbeddedLapack :)

What tool are you using in MATLAB?

I almost never use any such tools. I convert MATLAB code to C by hand most of the time. I rarely play with Matlab Coder, though. C code to MATLAB, I always do it by hand. I am not aware of any automatic tools.

I have always wanted to convert MATLAB code to C code, but it seems that MATLAB code to C code results code with dynamical memory allocation or code that uses to much memory. That's why I'm converting the code by hand.

Dynamic memory allocation is no-go for me as well. I think you can disable it. See [1]. I haven't tested [1], but I'd like to hear your feedback should you decide to try it.

[1] https://www.mathworks.com/help/coder/ug/disable-dynamic-memory-allocation-during-code-generation.html

from ccontrol.

DanielMartensson avatar DanielMartensson commented on May 31, 2024

Thank you for veryfing the results =)

Thank you for CControl and EmbeddedLapack :)

What tool are you using in MATLAB?

I almost never use any such tools. I convert MATLAB code to C by hand most of the time. I rarely play with Matlab Coder, though. C code to MATLAB, I always do it by hand. I am not aware of any automatic tools.

I have always wanted to convert MATLAB code to C code, but it seems that MATLAB code to C code results code with dynamical memory allocation or code that uses to much memory. That's why I'm converting the code by hand.

Dynamic memory allocation is no-go for me as well. I think you can disable it. See [1]. I haven't tested [1], but I'd like to hear your feedback should you decide to try it.

[1] https://www.mathworks.com/help/coder/ug/disable-dynamic-memory-allocation-during-code-generation.html

Thank you. I don't think I will use MATLAB tools that convert MATLAB code to C code because the code is quite messy and I don't have control over what I'm doing. Better to take extra time and write the code by hand, even if it's quite painfull.

Feel free to comment if you have suggestions.
I'm planning to SR-UKF to C code.
https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.80.1421&rep=rep1&type=pdf

Notice that SR-UKF can be used for training a neural network. Sounds interesting because neural networks are used for image classification.

from ccontrol.

ooaj avatar ooaj commented on May 31, 2024

Thank you. I don't think I will use MATLAB tools that convert MATLAB code to C code because the code is quite messy and I don't have control over what I'm doing. Better to take extra time and write the code by hand, even if it's quite painfull.

Fair enough. I agree.

Feel free to comment if you have suggestions.
I'm planning to SR-UKF to C code.
https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.80.1421&rep=rep1&type=pdf

Notice that SR-UKF can be used for training a neural network. Sounds interesting because neural networks are used for image classification.

I haven't fiddled with SR-UKF myself, but I know that it fixes the problem of negative definite covariance matrices in the algorithm. I would love to try and test your implementation once it is out!

from ccontrol.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.