request for a program

Discussion in 'C Programming' started by kumar, May 12, 2006.

  1. kumar

    kumar Guest

    i request the group to please send me " c programs " to find the matrix
    inverse and determinant for any order.
    kumar, May 12, 2006
    #1
    1. Advertising

  2. kumar said:

    > i request the group to please send me " c programs " to find the matrix
    > inverse and determinant for any order.


    #include <stdio.h>
    #include <string.h>

    #define MAXLINE 16384

    int main(void)
    {
    char haystack[MAXLINE] = {0};
    char needle[] = "the matrix inverse and determinant for any order";
    unsigned long line = 0;

    while(fgets(haystack, sizeof haystack, stdin) != NULL)
    {
    ++line;
    if(strstr(haystack, needle) != NULL)
    {
    printf("Hit on line %lu\n", line);
    }
    }

    return 0;
    }

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, May 12, 2006
    #2
    1. Advertising

  3. kumar

    Guest

    Why? I am sure you can find some open-source software that has the two
    operations you are looking for. The determinant is very easy to
    implement using a recursive algorithm. Expanding along a column or row
    is well-suited to recursion. Do you have any C experience?
    , May 12, 2006
    #3
  4. On Fri, 12 May 2006 04:23:34 -0700, kumar wrote:

    > i request the group to please send me " c programs " to find the matrix
    > inverse and determinant for any order.

    Somewhat off topic here.
    You might want to try sci.math.num-analysis.
    Why do you want to compute the inverse? If it's to solve
    a set of linear equations then there better ways to do that;
    try Googling for "LU decomposition" (which will also allow you
    to compute the inverse and determinant, if you really need them).
    Note that the straightforward way of computing the determinant takes
    on the order of n! operations, which makes it infeasible for anything
    but small matrices.
    Duncan
    Duncan Muirhead, May 12, 2006
    #4
  5. kumar

    Ben C Guest

    On 2006-05-12, <> wrote:

    [
    kumar> i request the group to please send me " c programs " to find the
    kumar> matrix inverse and determinant for any order.
    ]

    > Why? I am sure you can find some open-source software that has the two
    > operations you are looking for. The determinant is very easy to
    > implement using a recursive algorithm.


    The recursive algorithm is easy to implement, but likely to be very
    inefficient if the number of rows and columns is much more than 4.

    One way to work out the determinant I've heard of is to make the matrix
    upper-triangular, and then to multiply all the diagonal elements
    together.

    The upper-triangular matrix can also be used to invert the matrix.

    I have some octave source to hand for this, which could be converted to
    C fairly easily, if the OP can understand the octave. Doesn't include
    solving for the inverse using U, but that part isn't too hard.

    1;

    function U = gauss(M)
    % Make a matrix upper triangular
    for i = 2:rows(M)
    for j = 1:i - 1 % rows above you
    f = M(i, j) / M(j, j);
    M(i, :) -= M(j, :) * f;
    endfor
    endfor
    U = M;
    endfunction

    function d = my_det(M)
    % Compute determinant using Gaussian elimination followed by multiplying
    % diagonal elements together
    U = gauss(M);
    d = 1;

    for i = 1:rows(U)
    d *= U(i, i);
    endfor
    endfunction

    M = rand(6)

    % Using built-in det
    det(M)

    % Using our one, for comparison
    my_det(M)
    Ben C, May 12, 2006
    #5
  6. kumar

    Default User Guest

    wrote:

    > Why?



    Why what? See below.



    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, May 12, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    0
    Views:
    5,317
    Steve
    Jul 1, 2003
  2. Daniel Bass
    Replies:
    2
    Views:
    3,698
    dave wanta
    Jul 4, 2003
  3. Christian H
    Replies:
    1
    Views:
    856
    Natty Gur
    Jul 29, 2003
  4. Brian Birtle
    Replies:
    2
    Views:
    1,995
    John Saunders
    Oct 16, 2003
  5. George
    Replies:
    5
    Views:
    6,593
    George
    Apr 2, 2004
Loading...

Share This Page