calc cross product + shift proc from main
This commit is contained in:
parent
81f6ada779
commit
03bd148847
|
@ -1,12 +1,12 @@
|
|||
import strutils, sequtils
|
||||
|
||||
## prints a matrix to the standard output
|
||||
#? prints a matrix to the standard output
|
||||
proc printMatrix*(matrix: seq[seq[float]]) =
|
||||
let row: int = matrix.len
|
||||
for i in countup(1, row):
|
||||
echo matrix[i-1]
|
||||
|
||||
## get elements of matrix from standard input
|
||||
#? get elements of matrix from standard input
|
||||
proc fillMatrix*(matrix: var seq[seq[float]], row, col: int) =
|
||||
for i in countup(1, row):
|
||||
for j in countup(1, col):
|
||||
|
@ -16,8 +16,8 @@ proc fillMatrix*(matrix: var seq[seq[float]], row, col: int) =
|
|||
matrix[i-1].delete(0)
|
||||
echo matrix[i-1]
|
||||
|
||||
## calculate dot product
|
||||
proc dot*(matrix1: seq[seq[float]], matrix2: seq[seq[float]]): seq[seq[float]] =
|
||||
#? calculate dot product
|
||||
proc calcDot(matrix1: seq[seq[float]], matrix2: seq[seq[float]]): seq[seq[float]] =
|
||||
let col1: int = matrix1[0].len
|
||||
let row1: int = matrix1.len
|
||||
let col2: int = matrix2[0].len
|
||||
|
@ -36,4 +36,66 @@ proc dot*(matrix1: seq[seq[float]], matrix2: seq[seq[float]]): seq[seq[float]] =
|
|||
for j in countup(0, col2-1):
|
||||
for k in countup(0, col1-1):
|
||||
matrix[i][j] = matrix[i][j] + matrix1[i][k] * matrix2[k][j]
|
||||
return matrix
|
||||
return matrix
|
||||
|
||||
#? calculate cross product
|
||||
proc calcCross(vector1: array[3, float], vector2: array[3, float]): array[3, float] =
|
||||
let i: float = vector1[1]*vector2[2] - vector1[2]*vector2[1]
|
||||
let j: float = vector1[2]*vector2[0] - vector1[0]*vector2[2]
|
||||
let k: float = vector1[0]*vector2[1] - vector1[1]*vector2[0]
|
||||
let vector = [i, j, k]
|
||||
return vector
|
||||
|
||||
proc dot*() =
|
||||
#? record first matrix
|
||||
echo "Enter number of rows in the first matrix:"
|
||||
let r1: int = parseInt(readLine(stdin))
|
||||
echo "Enter number of columns in the first matrix:"
|
||||
let c1: int = parseInt(readLine(stdin))
|
||||
var m1 = newSeqWith(r1, newSeq[float](c1))
|
||||
procedures.fillMatrix(m1, r1, c1)
|
||||
|
||||
echo ""
|
||||
|
||||
#? record second matrix
|
||||
echo "Enter number of rows in the second matrix:"
|
||||
let r2: int = parseInt(readLine(stdin))
|
||||
echo "Enter number of columns in the second matrix:"
|
||||
let c2: int = parseInt(readLine(stdin))
|
||||
var m2 = newSeqWith(r2, newSeq[float](c2))
|
||||
procedures.fillMatrix(m2, r2, c2)
|
||||
|
||||
#? resultent matrix
|
||||
var m = newSeqWith(0, newSeq[float](0))
|
||||
m = calcDot(m1, m2) #? calculate dot product (in procedures)
|
||||
|
||||
echo "\nFirst matrix is:"
|
||||
printMatrix(m1)
|
||||
echo "\nSecond matrix is:"
|
||||
printMatrix(m2)
|
||||
echo "\nResult matrix is:"
|
||||
printMatrix(m)
|
||||
|
||||
proc cross*() =
|
||||
type
|
||||
vector = array[3, float]
|
||||
var
|
||||
v1: vector
|
||||
v2: vector
|
||||
|
||||
echo "Enter numbers in the first vector:"
|
||||
for i in countup(0, 2):
|
||||
echo "Enter item:"
|
||||
v1[i] = parseFloat(readLine(stdin))
|
||||
|
||||
echo "Enter numbers in the second vector:"
|
||||
for i in countup(0, 2):
|
||||
echo "Enter item:"
|
||||
v2[i] = parseFloat(readLine(stdin))
|
||||
|
||||
#? resultent vector
|
||||
let v = calcCross(v1, v2)
|
||||
|
||||
echo v1, " × ", v2
|
||||
echo "\nResult vector is:"
|
||||
echo v
|
||||
|
|
Loading…
Reference in a new issue