Page 1 of 1

Matrix determinant

Posted: Mon Apr 20, 2015 7:46 am
by Elessar
Hello all,

I have a 2-dim cube with equivalent dimensions (an NxN matrix) and need to calculate it's determinant. Did anybody do this using only rules, without processes?

Re: Matrix determinant

Posted: Mon Apr 20, 2015 8:36 am
by rmackenzie
Elessar wrote:I have a 2-dim cube with equivalent dimensions (an NxN matrix) and need to calculate it's determinant.
If N was only going to be 2, or 3, then you could do something like this with the inclusion of a measures dimension:

Code: Select all

SKIPCHECK;
['Measure':'Input'] = N: STET;

# 2x2 grid
['Row':'a', 'Column':'a', 'Measure':'Output'] = N:
  IF ( DB ( 'Calc Control', 'Grid Size', 'Number' ) = 2,
    ( ['Row':'a','Column':'a','Measure':'Input'] * ['Row':'b','Column':'b','Measure':'Input'] )
    -
    ( ['Row':'a','Column':'b','Measure':'Input'] * ['Row':'b','Column':'a','Measure':'Input'] ),
    CONTINUE 
  );

# 3x3 grid
['Row':'a', 'Column':'a', 'Measure':'Output'] = N:
  IF ( DB ( 'Calc Control', 'Grid Size', 'Number' ) = 3,
    ( ['Row':'a','Column':'a','Measure':'Input']
    *
    ( ['Row':'b','Column':'b','Measure':'Input'] * ['Row':'c','Column':'c','Measure':'Input'] )
    -
    ( ['Row':'b','Column':'c','Measure':'Input'] * ['Row':'c','Column':'b','Measure':'Input'] ) )
    -
    ( ['Row':'a','Column':'b','Measure':'Input']
    *
    ( ['Row':'b','Column':'a','Measure':'Input'] * ['Row':'b','Column':'c','Measure':'Input'] )
    -
    ( ['Row':'b','Column':'c','Measure':'Input'] * ['Row':'c','Column':'a','Measure':'Input'] ) )
    +
    ( ['Row':'a','Column':'c','Measure':'Input']
    *
    ( ['Row':'b','Column':'a','Measure':'Input'] * ['Row':'c','Column':'b','Measure':'Input'] )
    -
    ( ['Row':'b','Column':'b','Measure':'Input'] * ['Row':'c','Column':'a','Measure':'Input'] ) ),
    CONTINUE 
  );

# no calcs where grid size is > 3 % non-a and non-a combos
['Measure':'Output'] = N: STET;

FEEDERS;
['Measure':'Input'] => ['a', 'a', 'Output'];
If you don't know N, then I'd prefer to use TI as it is likely you can write one TI to handle any N.

Why don't you know N?

Re: Matrix determinant

Posted: Mon Apr 20, 2015 8:54 am
by Elessar
Hi and thanks for reply

I'm going to do the Reciprocal Method of cost allocation from one Dep. to another: http://classes.bus.oregonstate.edu/Spri ... r%2012.htm
The most difficult there is to solve the system of linear equations.

N is not fixed: "Department" a dinamically updated dimension

Re: Matrix determinant

Posted: Mon Apr 20, 2015 9:16 am
by rmackenzie
Right, I see - so the number of departments is going to be unknown.

There's a bit of VB here that solves an NxN matrix determinant problem in 39 lines of code. It looks possible to rewrite in TI, at a glance. So, why not leverage that and hook it to an Action Button and get your users to use that?

Code: Select all

Function determinant(Matrix() As Single, Norder As Integer, deter As Single)
Dim k, k1, i, j As Integer
Dim save As Single
Dim check As Boolean
deter = 1
For k = 1 To Norder
 If Matrix(k, k) = 0 Then
  j = k
  Do
   check = True
   If Matrix(k, j) = 0 Then
    If j = Norder Then
     deter = 0
     Exit Function
    End If
    check = False
    j = j + 1
   End If
   If Matrix(k, j) <> 0 Then
    For i = k To Norder
     save = Matrix(i, j)
     Matrix(i, j) = Matrix(i, k)
     Matrix(i, k) = save
    Next i
    deter = -deter
   End If
  Loop While check = False
 End If
 deter = deter * Matrix(k, k)
 If k - Norder < 0 Then
  k1 = k + 1
  For i = k1 To Norder
   For j = k1 To Norder
    Matrix(i, j) = Matrix(i, j) - (Matrix(i, k) * Matrix(k, j) / Matrix(k, k))
   Next j
  Next i
 End If
Next k
End Function