Updated 2012-06-13 03:02:59 by RLE

Example of use of the math::linearalgebra package to solve a question posed on [1]

This text copied directly from the above reference:
  Example:
  A man buys 3 fish and 2 chips for £2.80
  A woman buys 1 fish and 4 chips for £2.60
  How much are the fish and how much are the chips?

Use the linear algebra package thus:
  package require math::linearalgebra
  set mt [math::linearalgebra::mkMatrix 2 2 0.0]  ;# there are 2 transactions buying 2 items
  set vc [math::linearalgebra::mkVector 2 0.]     ;# 2 transactions
  math::linearalgebra::setelem mt 0  0 3          ;# 3 fish in transaction 1...
  math::linearalgebra::setelem mt 0  1 2          ;# 2 chips
  math::linearalgebra::setelem mt 1  0 1          ;# 1 fish in transaction 2
  math::linearalgebra::setelem mt 1  1 4          ;# 4 chips (this is a rather unbalanced meal)
  math::linearalgebra::setelem vc 0 2.8           ;# cost of transaction 1
  math::linearalgebra::setelem vc 1 2.6           ;# and transaction 2
  math::linearalgebra::solveGauss $mt $vc         ;# calculate the result

And the result:
  0.6 0.500000000001

or the fish cost 0.60 (rather cheap) and the chips 0.50. As given in the reference above.

An even shorter version is:
  package require math::linearalgebra
  set mt {{3 2} {1 4}}
  set vc {2.8 2.6}
  math::linearalgebra::solveGauss $mt $vc

Do the simultaneous equations have a solution? Only if the Matrix determinant is not zero. Can we have a determinant function added to the linearalgebra package please!

Here is a matrix with a zero determinant:
  set mt {{3 2} {6 4}}
  math::linearalgebra::solveGauss $mt $vc

the stdout output is:
 divide by zero

- not very helpful but the equations do not allow a unique solution. The matrix is equivalent to:
  3 x + 2 y = 2.8
  6 x + 4 y = 2.6

Twice the first equation minus the second equation leaves zero = 3.