Determinant solver ( order 3 )

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Determinant solver ( order 3 )

13 Oct 2013, 11:38

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
UpDia_sum := 0
downdia_sum := 0
UpDia := 1
downDia := 1
col := 0
array =
(
1 2 1
3 4 2
3 4 6
)
  _array := []
  for k, v in StrSplit(array, "`n")
	for M, q in StrSplit(v, " ")
_array[k, M] := q
StringSplit, rows, array, `n
loop % rows0
	Col := col + 1
if ( col != rows0 )
{
	MsgBox wrong Input
	ExitApp
}

;MsgBox col is %col% and row is %rows0%
;;;adding more lines;;;;;;;;
a := 1
b := 1
R := 1
C := Col + 1
loop % rows0 
		
	{

			loop % col - 1

				{
				_array[R, C] := _array[a, b]
				;MsgBox %R% and %c% so far generated 
 ; 				MsgBox % _array[R, C]
				b := b + 1
				c := C+1
				}
	
	R := R + 1
	a := a + 1
	b := 1
	C := Col + 1
	}
;;;;;;;;;;;; duplicate lines generated for arrays;;;;;;;;;;;;;;;;;

K := 1
M := 1
	
MM := 1
loop %col%
{
;MsgBox initial array for %k% and %m% and %a_index%
M := A_index 
Loop
{

;MsgBox M is %M%
					if ( _array[K, M] != "" )
						{
					;		MsgBox we are at %k% and %m%
						UpDia := Updia * _array[K, M]
					;	MsgBox % _array[K, M]
					;MsgBox %updia% is the result so far
						K := K + 1, M := M + 1
						}
								else 
							{
							;MsgBox %updia% %k% is k and %m% m
							Updia_Sum := Updia_sum + updia
							K := 1, updia := 1 
							goto, label	
							}

}
label:
}
;MsgBox %updia_sum% is the sum of up diagonals ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;down diagonal sum to be calculated now;;;;;;;;;;;;;;;;;

K := col
M := 1
	
loop %col%
{
;	MsgBox	%A_index%
;MsgBox initial array for %k% and %m% and %a_index%
M := A_index 
Loop
{

;MsgBox M is %M%
					if ( _array[K, M] != "" )
						{
						;	MsgBox we are at %k% and %m%
						downDia := downdia * _array[K, M]
					;	MsgBox % _array[K, M]
					;MsgBox %downdia% is the result so far
						K := K - 1, M := M + 1
						}
								else 
							{
							;MsgBox %downdia% %k% is k and %m% m
							downdia_Sum := downdia_sum + downdia
							K := col, downdia := 1 
							goto, label2	
							}

}
label2:
}
;MsgBox %downdia_sum% is the sum of up down diagonals ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


MsgBox % updia_sum - downdia_sum
John ... you working ?
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Determinant solver ( order 3 )

13 Oct 2013, 12:25

mine returns -8.
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Determinant solver ( order 3 )

13 Oct 2013, 20:43

Guest10 wrote:mine returns -8.
yup! same here.

Can calculate for any determinant of order 3. Change numbers in the array = ( your determinant elements)
accordingly

when i was making it i though the diagonal method could be used to solve determinant for any order so i made accordingly, but it gives correct answer for 3 order determinants only, :(

i was very sad to append (order 3) in the title .. otherwise it's a one line code.

anyways it is ok.
John ... you working ?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: submeg and 101 guests