[misc.wanted] Need matrix inversion C routine.

wkb@cbnews.ATT.COM (Wm. Keith Brummett) (04/21/89)

    I have need of a small, fast routine in C language that will invert
    matrices of order <= 4.  (Actually, I'll take a large, slow one that
    does order 2 or 3).  Does anyone have something like this that they
    would care to send me?  If it matters, it will be used in a small
    graphics project.

    BTW, can anyone tell me why it is that every language except C seems
    to have standard subroutines to do this?

    Thanks in advance!

-- 
    | Wm. Keith Brummett         (614) 860-3187          AT&T, Room 2B-252 |
    | att!cbnews!wkb               Cornet 353          6200 East Broad St. |
    | or, wkb@cbnews.ATT.COM                       Columbus, OH 43213-1550 |
    `----------------------------------------------------------------------'

k1wolcot@pikes.Colorado.EDU (Kenneth A. Wolcott) (04/21/89)

In article <5785@cbnews.ATT.COM>, wkb@cbnews.ATT.COM (Wm. Keith Brummett) writes:
 
 [ ... lines deleted ... ]
 
>     BTW, can anyone tell me why it is that every language except C seems
>     to have standard subroutines to do this?

 [ ... lines deleted ... ]

I did not see any matrix inversion routines "BUILT-IN" since I started in
Pascal in 1981 -- let's see -- I've looked at, used or read about as many as
twenty different PC-based or mini-computer Pascal compilers -- none had matrix
inversion routines!  Ada doesn't have one (and people accuse Ada of being the
"kitchen-sink" language) -- I must have missed it when I looked at Prime Pl/1
-- I don't think any of our 7 FORTRAN compilers have it...

I think that you are after a LIBRARY function (not BUILT-IN) for matrix
inversion.  If so, that is a installation-dependent characteristic!  Someone at
site #1 has a FORTRAN compiler on a PC with that library -- someone at site #2
has the same compiler and the same hardware, but doesn't have that library.
Same thing with "C".  Same thing with Ada, Pascal, Modula-2, PL/1, COBOL --
you name the language -- you'll find different libraries installed.  ANSI
never (I don't think so) did standardize the libraries for each language!  How
could it be done?  It might be real nice to be able to go to six different
sites and get identical library support!

Kenneth A. Wolcott
University of Colorado at Denver
Computing Services Advising & Operations

"k1wolcot@pikes.colorado.edu"  or
"kwolcott@pikes.colorado.edu"  or
"kwolcott@copper.colorado.edu" or
"kwolcott@orphan.colorado.edu" or
"kawolcott@cudenver.bitnet"

==========================================================================

gwyn@smoke.BRL.MIL (Doug Gwyn) (04/21/89)

In article <5785@cbnews.ATT.COM> wkb@cbnews.ATT.COM (Wm. Keith Brummett) writes:
>    I have need of a small, fast routine in C language that will invert
>    matrices of order <= 4.

/* The following do not attempt to handle singularities or ill-conditioning. */
void
Inv1( double a[1][1], double b[1][1] )
	{
	b[0][0] = 1.0 / a[0][0];
	}
void
Inv2( double a[2][2], double b[2][2] )
	{
	register double	d = a[0][0] * a[1][1] - a[0][1] * a[1][0];
	b[0][0] =  a[1][1] / d;
	b[0][1] = -a[1][0] / d;
	b[1][0] = -a[0][1] / d;
	b[1][1] =  a[0][0] / d;
	}
void
Inv3( double a[3][3], double b[3][3] )
	{
	double		m00 = a[1][1] * a[2][2] - a[2][1] * a[1][2];
	double		m01 = a[1][2] * a[2][0] - a[2][2] * a[1][0];
	double		m02 = a[1][0] * a[2][1] - a[2][0] * a[1][1];
	register double	d = a[0][0] * m00 + a[0][1] * m01 + a[0][2] * m02;
	b[0][0] = m00 / d;
	b[0][1] = m01 / d;
	b[0][2] = m01 / d;
	b[1][0] = (a[2][1] * a[0][2] - a[0][1] * a[2][2]) / d;
	b[1][1] = (a[2][2] * a[0][0] - a[0][2] * a[2][0]) / d;
	b[1][2] = (a[2][0] * a[0][1] - a[0][0] * a[2][1]) / d;
	b[2][0] = (a[0][1] * a[1][2] - a[1][1] * a[0][2]) / d;
	b[2][1] = (a[0][2] * a[1][0] - a[1][2] * a[0][0]) / d;
	b[2][2] = (a[0][0] * a[1][1] - a[1][0] * a[0][1]) / d;
	}
void
Inv4( double a[4][4], double b[4][4] )
	{
	/* XXX -- you provide this yourself, I'm getting tired */
	}

>    BTW, can anyone tell me why it is that every language except C seems
>    to have standard subroutines to do this?

Very few languages have standard matrix inversion functions.
APL is about the only one I know of.
Certainly not Fortran or Pascal.