-4
\$\begingroup\$

Write a full program to calculate the inverse of a 4-by-4 matrix.

Rules

The sixteen values of the initial matrix must be hard-coded to variables, as follows:

_       _
|a b c d|
|e f g h|
|i j k l|
|m n o p|
-       -

Then print the values of the inverse matrix, row by row, with each number separated by some delimiter. You may assume that the given matrix has an inverse.

The results must be completely accurate, limited only by the programming language. For example, you may assume that (1 / 3) * 3 = 1, there's no such thing as integer overflow, and so on.

You may use only the following operations:

  • Addition - 10 points
  • Subtraction - 15 points
  • Multiplication - 25 points
  • Division - 50 points
  • Assignment to a variable - 2 points
  • Printing a variable (not an expression) and a delimiter - 0 points
  • Recalling a variable or numeric constant - 0 points

For example: x = (a + b) / (a - 2) would score 77 points.

To clarify, you may not use any other operations or structures, such as arrays, functions (outside the main method, if needed), if-statements, etc.

The initial hard-coding of the variables does not give any points.

Fewest points wins.

Testcases

Note that all testcases can be used in reverse.

a=1; b=1; c=2; d=-1; e=-2; f=0; g=-2; h=1; i=-1; j=2; k=1; l=0; m=2; n=-1; o=0; p=2

Print, in order: 9 7 -4 1 10 8 -4 1 -11 -9 5 -1 -4 -3 2 0

a=1; b=2; c=3; d=0; e=2; f=2; g=1; h=0; i=4; j=4; k=4; l=4; m=2; n=1; o=-2; p=0

Print, in order: -5 7 0 -4 6 -8 0 5 -2 3 0 -2 1 -2 .25 1

a=-.25; b=.5; c=0; d=.5; e=1; f=.25; g=-.5; h=0; i=-1; j=-.5; k=.5; l=.25; m=.25; n=1; o=0; p=0

Print, in order: -2 4 4 2 .5 -1 -1 .5 -3.75 5.5 7.5 4.25 .5 3 3 .5

\$\endgroup\$
8
  • \$\begingroup\$ Just checking, but creating arrays is not allowed, correct? \$\endgroup\$ Feb 4, 2015 at 22:57
  • \$\begingroup\$ Can we also assume the variables to be stored in an array? I assume we may also use other structures like loops/conditionals etc, do we? \$\endgroup\$
    – flawr
    Feb 4, 2015 at 23:16
  • \$\begingroup\$ Is the score for an operation counted each time the operation is written in the code, or each time it is performed during execution? \$\endgroup\$
    – feersum
    Feb 4, 2015 at 23:31
  • \$\begingroup\$ Can our code fail on a measure zero subset of matrices that cause a divide by zero even though they are invertible? Gaussian elimination is likely to run into this. If not, can we use an if statement? \$\endgroup\$
    – xnor
    Feb 5, 2015 at 0:28
  • 3
    \$\begingroup\$ It makes a huge difference. \$\endgroup\$
    – feersum
    Feb 5, 2015 at 0:57

1 Answer 1

2
\$\begingroup\$

Java: 3974

Now this is a totally new version of the prgoram, that satisfies the updated conditions. Note that I did use an array for printing in this version here, but I counted as if every assignment was an own new variable. The for loops save some space for those who want to see the code here...

I am using the only possible approach (i think) under the restrictions via minor determinats that form the adjoint matrix.

For those who want to check the correctness of the formula.

Stats:

= :  2* 42
+ : 10* 44
- : 15* 60
* : 25*100
/ : 50*  1

public static void main(String[] args){
    double a=4,b=1,c=1,d=2,
           e=4,f=4,g=1,h=5,
           i=2,j=1,k=5,l=1,
           m=4,n=2,o=4,p=3;
    double af=a*f;
    double kp=k*p;
    double lo=l*o;
    double ag=a*g;
    double jp=j*p;
    double ln=l*n;
    double ah=a*h;
    double jo=j*o;
    double kn=k*n;
    double be=b*e;
    double bg=b*g;
    double lm=l*m;
    double bh=b*h;
    double io=i*o;
    double km=k*m;
    double ce=c*e;
    double cf=c*f;
    double ip=i*p;
    double ch=c*h;
    double in=i*n;
    double jm=j*m;
    double de=d*e;
    double df=d*f;
    double dg=d*g;

    double z = 1/(af*(kp-lo)+ag*(ln-jp)+ah*(jo-kn)+be*(lo-kp)
               +bg*(ip-lm)+bh*(km-io)+ce*(jp-ln)+cf*(lm-ip)
               +ch*(in-jm)+de*(kn-jo)+df*(io-km)+dg*(jm-in));
    
    double[] inv = {h*(jo-kn)+g*(ln-jp)+f*(kp-lo) , d*(kn-jo)+b*(lo-kp)+c*(jp-ln) , (ch-dg)*n+(df-bh)*o+(bg-cf)*p , (dg-ch)*j+(bh-df)*k+(cf-bg)*l,
            h*(km-io)+g*(ip-lm)+e*(lo-kp) , d*(io-km)+c*(lm-ip)+a*(kp-lo) , (dg-ch)*m+(ah-de)*o+(ce-ag)*p , (ch-dg)*i+(de-ah)*k+(ag-ce)*l,
            h*(in-jm)+f*(lm-ip)+e*(jp-ln) , d*(jm-in)+a*(ln-jp)+b*(ip-lm) , (bh-df)*m+(de-ah)*n+(af-be)*p , (df-bh)*i+(ah-de)*j+(be-af)*l,
            g*(jm-in)+e*(kn-jo)+f*(io-km) , c*(in-jm)+b*(km-io)+a*(jo-kn) , (cf-bg)*m+(ag-ce)*n+(be-af)*o , (bg-cf)*i+(ce-ag)*j+(af-be)*k};

    for(int u=0;u<16;u++){
        inv[u] = inv[u]*z;  //counting as 16 assignments and 16 multiplicatiosn
        
    }
    //printing all the values
    for(int u=0;u<16;u++){
        if(u%4 == 0){
            System.out.println("");
        }
    System.out.print(inv[u]+" ");

    }
    
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.