Sign in to follow this  
Followers 0
Sharpienero

[c] Make this algorithm to take the product of matrices with one unknown more efficient?

3 posts in this topic

Hi,

 

I wrote this program and it works properly, but I'm looking for ways to make it somewhat more efficient and or improve readability.

 

Here is the code on hastebin.

#include <stdio.h>

int main (void) {

    float a[2][2];
    float constant[2];
    float x[2];
    double a2;
    double determinant;

    puts("Give 6 numbers seperated with a space (i.e 1 2 3 4 5 6): ");
    scanf("%f %f %f %f %f %f", &a[0][0],  &a[0][1], &constant[0], &a[1][0],  &a[1][1], &constant[1]);

    determinant = (a[0][0] * a[1][1]) - (a[0][1] * a[1][0]);
   
    if (determinant == 0) {
    	puts("Too complicated!");
    	return 0;
    }

    a2 = a[0][0];
    a[0][0] = ((a[1][1]) / (determinant));
    a[1][1] = ((a2) / (determinant));
    a[1][0] = ((-a[1][0]) / (determinant));
    a[0][1] = ((-a[0][1]) / (determinant));
    x[0] = (a[0][0] * constant[0]) + (a[0][1] * constant[1]);
    x[1] = (a[1][0] * constant[0]) + (a[1][1] * constant[1]);

    printf("x = %f\n" , x[0]);
    printf("y = %f\n" , x[1]);
}

I know that the parenthesis are majorly unneeded, but they help me logically see things. Feel free to show me a new way to stylize things.

0

Share this post


Link to post
Share on other sites

This is mathematically wrong. You're multiplying a 2x2 matrix by a 1x2 matrix; it should be 2x2 multiplied by a 2x1 matrix. Doesn't change anything, though. As well as this, your code states that if the determinant is equal to zero, it is too complicated; it should say that there isn't a solution. When det = 0, there is no inverse, therefore there are zero solutions.

 

For such a simple problem as this, you can't make your algorithm more efficient. Rather, there's no point considering this should take even a 20 year old pocket calculator about 0.005ms. The hardest part for the processor to carry out is the printf().

 

I see you're trying to calculate the inverse matrix then multiply both sides. I guess you could try forming two simultaneous equations then developing an algorithm to solve them - if you're up to the task.

1

Share this post


Link to post
Share on other sites

This is mathematically wrong. You're multiplying a 2x2 matrix by a 1x2 matrix; it should be 2x2 multiplied by a 2x1 matrix. Doesn't change anything, though. As well as this, your code states that if the determinant is equal to zero, it is too complicated; it should say that there isn't a solution. When det = 0, there is no inverse, therefore there are zero solutions.

 

For such a simple problem as this, you can't make your algorithm more efficient. Rather, there's no point considering this should take even a 20 year old pocket calculator about 0.005ms. The hardest part for the processor to carry out is the printf().

 

I see you're trying to calculate the inverse matrix then multiply both sides. I guess you could try forming two simultaneous equations then developing an algorithm to solve them - if you're up to the task.

Ah, you're totally right! I didn't realize that I was multiplying mxn by pxn rather than mxn by nxp. The resulting output should be a mxp, so I can see why you said it's mathematically incorrect. Do you think that I should make it multiply correctly?

 

I put that it was too complicated due to the fact that it's what the professor wanted us to write. I'm thinking back in my discrete mathematics class, and yes, I do recall the information regarding the inverse of the matrices. 

 

Is it also possible to do this with 2-two dimensional arrays?

 

I was thinking of making an algorithm to multiply larger matrices (possibly 3x3?), but that's kind of hard for me to even fathom. Thank you for your input!

0

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  
Followers 0