From 3e623748449f6d03b5d947946247cd92594ba9ce Mon Sep 17 00:00:00 2001 From: array-in-a-matrix Date: Tue, 22 Nov 2022 19:53:27 -0500 Subject: [PATCH] dot product --- src/main.nim | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main.nim b/src/main.nim index 133a950..f467c99 100644 --- a/src/main.nim +++ b/src/main.nim @@ -1,4 +1,5 @@ -import strutils, sequtils, os, procedures #, strformat +import strutils, sequtils, os, procedures, strformat + var operation: string case paramStr(0): @@ -10,7 +11,7 @@ case paramStr(0): echo "Parameter is cross" else: # TODO: ask user to preform dot or cross product - echo "maultrix" + echo "multrix" echo "Enter number of rows in the first matrix:" @@ -31,20 +32,31 @@ var m2 = newSeqWith(r2, newSeq[float](c2)) procedures.fillMatrix(m2, r2, c2) -var m = newSeqWith(0, newSeq[float](0)) # ? double check this +var r, c: int # TODO: calculate dot product (in procedures) -for i in countup(1, r1): - for j in countup(1, c1): - m[i-1].add( m1[i-1][j-1] * m2[j-1][i-1] + m1[i][j] * m2[j][i] ) # ? double check this - m[i-1].delete(0) +if c1 == r2: + c = c2 + r = r1 +else: + quit "Matrix dimensions mismatched, operation invalid!", QuitFailure + +var m = newSeqWith(r, newSeq[float](c)) + +for i in countup(0, r1-1): + for j in countup(0, c2-1): + for k in countup(0, c1-1): + m[i][j] = m[i][j] + m1[i][k] * m2[k][j] + # TODO: calculate cross product (in procedures) + echo "\nFirst matrix is:" procedures.printMatrix(m1, r1) + echo "\nSecond matrix is:" procedures.printMatrix(m2, r2) -# TODO: print result to stdout -procedures.printMatrix(m, r1) \ No newline at end of file +echo "\nResult matrix is:" +procedures.printMatrix(m, r) \ No newline at end of file