1
$\begingroup$

image of the code I have tried changing the variables, reevaluating the cells, but it just keeps giving me false. The weird part is that it was originally true and it changed to false, randomly.I did not want to post the whole thing because I am solving an 18 by 18 matrix. I am using equivalent to prove that the solutions are the same.

A={{-581.5, -744.9, 338.2, 342., 200.9, -8.9, 697.2, -339.2, 
128.7, -736.5, 286.6, 464.6, -811.7, -399.5, 977.4, 21.9, 
3.4, -500.6}, {-226.9, -16.7, 496.3, 759.7, -257.4, 514.7, -995.9, 
630.2, -346.4, -653.8, -258.6, -697., 268.6, -69.7, 
486.4, -820., -14.8, -950.5}, {-611.2, -167.8, -749.1, -158.5, \
-513.5, 878.6, 938.3, -782.4, -100.9, 657., -430., -825.1, -956.3, 
491.4, 250.9, -501.4, -596., 75.3}, {462.4, 945.4, -771.5, 
606.9, -80.4, 582.8, 209.7, 613.3, -27.9, 352.3, 19.4, 476.2, 983., 
872.8, 246.6, -313.2, -715.7, 459.7}, {-686., 51.6, 364.7, 
626.4, -876.4, 938.9, -984.2, 36.1, 610., 331.7, -459.6, 961.7, 
847., -303.5, 272., 234.3, -32.2, -147.1}, {134.9, -637.7, 542.6, 
321.5, -852.9, -484.6, 571.9, -536.6, 709.8, 520.1, 919.9, -518.4, 
268.9, -326.1, -177.2, -643.4, 781.9, 971.5}, {376.9, 480.9, 
489.9, -281.5, 921., 724., -252.5, -890.4, -632.5, 754.2, -211.7, 
349.3, 62.9, 772.3, 15.7, 399.1, -415.3, -299.9}, {-570.6, -348.1, 
248.4, -215.1, -749.5, 562.4, -441.2, -795.8, 414., -872.2, 957.4, 
445.3, -533., 636.9, -759.6, 587.4, -567.9, -545.}, {-332.6, -374.9,
695.9, 704., -878.3, 264.1, 518.6, -626.2, -706.9, 154.8, 
835., -725.6, 414.8, 101.1, 642.9, 855., 
159.1, -807.5}, {585., -36.2, -932.6, -995.6, 430.5, -378.4, 
62.6, -662.3, 129.1, 373.8, 807.2, 749.1, -561.3, 855.8, -258.7, 
649.8, -404.4, 271.1}, {-297.8, 635.7, 585.2, 106.3, 806.6, 740., 
827.3, 649.6, 409.6, 532.4, -510., -74.3, 641.7, 318.8, 
490.1, -582.1, -63.7, 977.1}, {175.8, 844.7, 387.8, 753.9, -681.1, 
835.6, 215.3, -117.9, -32.7, 383.6, 
541.8, -971.8, -970.6, -719.9, -667., -141.2, -131.1, 
108.4}, {-288.7, -144.7, -368.7, 666.1, 578., -270., -316.9, 273.5, 
322.3, -987.8, 916.1, 332.4, -106.4, 311.2, -557.2, 352.1, 171., 
677.8}, {-608.4, -336., 428., 4.2, 992.2, 
243.6, -103.1, -585.2, -975.7, -853.5, 687.9, 443.1, -1.9, -788.2, 
910.9, 292.3, 760.5, -508.3}, {740.7, -752.2, -149.7, 127.2, 
93.7, -957.2, -27.4, -54.4, 934.1, 786.7, -367.6, 
254.1, -57.7, -186.9, 276.9, -903.6, 370.4, -443.6}, {592.2, -266.1,
280.9, 307.6, 393.4, 373.1, 982.4, -794.8, -515.1, 
752.2, -104.7, -568.4, 352.4, 710.1, -327.4, -248.7, -977.8, 
583.2}, {-201.5, 527.2, 766.5, -323.4, 
790.7, -468.4, -201.7, -748.5, -145.5, -650.8, -227.2, 
29.7, -854.2, -778.6, -154.3, -220.9, -153.2, -337.}, {-455.4, \
-933.3, 63.4, 961.9, 401.3, 823.3, 538.6, 789.7, 662.7, 116.1, -67.5, 
280.5, 954., 579., -551.6, -58.3, 44.5, -511.8}}

B={-377.3, 661.8, -352.3, -273., 851.5, -724.9, 204., -85., 162.1, 
322.2, -657.9, 100.9, 946.7, 881.4, 393.7, 285.2, 902.6, 604.7}

(*module*)
cramers[A_, b_] := Module[{d = Det[A], a}, 
Table[a = A;[[All, k]] = b; Det[a]/d, {k, Length[A]}]]
R51= cramers[A,B] 
$\endgroup$
10
  • $\begingroup$ What do you get when you evaluate Max[Abs[ B - A.R51 ]]? $\endgroup$
    – Jason B.
    Nov 12, 2018 at 18:42
  • $\begingroup$ We would need the actual inputs to be able to figure out where the issue is. Do you have the original code you could upload? $\endgroup$
    – kickert
    Nov 12, 2018 at 18:59
  • 2
    $\begingroup$ A rule of thumb in programming is that you should not compare directly whether two floating point numbers are equal. An easy generalisation of this principle is that you should not compare whether two lists of floating point numbers are equal. Look at the differences of the corresponding slots and see whether they are smaller than a chosen epsilon. $\endgroup$ Nov 12, 2018 at 19:01
  • 1
    $\begingroup$ Or test whether Norm[A.R51 - B] is less than some epsilon $\endgroup$
    – Bob Hanlon
    Nov 12, 2018 at 19:40
  • $\begingroup$ @kickert i didnt want to put the whole thing because it is long, but i just posted it $\endgroup$ Nov 12, 2018 at 19:57

2 Answers 2

1
$\begingroup$

First, the linear algebra method you want to implement is the LinearSolve function, you can obtain r51 as

r51 = LinearSolve[aa, bb];

Anyway, you get the same problem

bb === aa.r51
False

However, this is due to numerical problems, because Max[Abs[bb - aa.r51]] gives around 10^-13.

You can overcome this problem by setting Rationalized versions of A and b

aar = Rationalize[aa];
bbr = Rationalize[bb];
r51r = LinearSolve[aar, bbr];

bbr === aar.r51r
True
$\endgroup$
0
$\begingroup$

The difference B - A.R51 shows all values on the order of 10^-12. This is pretty much zero for floating point numbers. If you want to get exactly zero, you can rationalize the calculations. First rationalize your cramers function

cramers[A_, b_] := Module[{d = Det[A], a}, 
   Table[a = A; a[[All, k]] = b; Rationalize[Det[a]/d], {k, Length[A]}]];

Then:

aRat = Rationalize[A];
bRat = Rationalize[B];
rRat = Rationalize[cramers[aRat, bRat]];
bRat == aRat.rRat
True

and you can verify that bRat - aRat.rRat is a vector of all zeroes.

$\endgroup$
1
  • $\begingroup$ Also possible to use Threshold to clip the difference instead of pulling out the heavy duty symbolics on Rationalize $\endgroup$
    – b3m2a1
    Dec 13, 2018 at 2:07

Your Answer

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

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