Compare commits

...

14 commits

Author SHA1 Message Date
array-in-a-matrix 1514f98c1a bumped version 2024-02-27 14:02:24 -05:00
array-in-a-matrix 5c954b63b1 accept fractions 2024-02-27 14:01:33 -05:00
array-in-a-matrix 8ebea26bdd bumped version 2024-02-26 17:17:08 -05:00
array-in-a-matrix a51029284c Merge branch 'main' of https://git.arrayinamatrix.xyz/array-in-a-matrix/multrix 2024-02-26 17:12:39 -05:00
array-in-a-matrix cffe9ff7ca added logic to handle scalar multiplication 2024-02-26 17:12:17 -05:00
array-in-a-matrix cfd70851c6 styled text outputed 2024-02-26 15:53:28 -05:00
array-in-a-matrix e9d4f763e0 Update README.md 2024-01-31 12:56:18 -05:00
array-in-a-matrix ec084ce210 Update .gitignore 2024-01-31 12:43:27 -05:00
array-in-a-matrix 1fd73f0195 fixed 2024-01-31 12:41:44 -05:00
array-in-a-matrix 7267f7159c PKGBUILD git 2024-01-31 12:36:59 -05:00
array-in-a-matrix e694bcf683 ignore compressed archives 2024-01-31 12:19:58 -05:00
array-in-a-matrix 15c13a2283 Merge branch 'main' of https://git.arrayinamatrix.xyz/array-in-a-matrix/multrix 2024-01-31 12:04:48 -05:00
array-in-a-matrix 2f27d2fd87 build using nimble instead + create nimble package 2024-01-31 12:04:36 -05:00
array-in-a-matrix 034a7facc9 Update README.md 2023-08-15 22:28:51 -04:00
8 changed files with 157 additions and 76 deletions

3
.gitignore vendored
View file

@ -4,3 +4,6 @@ nimblecache/
htmldocs/
bin/
doc/
multrix
*.tar.gz
*.pkg.tar.zst

View file

@ -1,29 +0,0 @@
BIN_DIR := bin/
SRC_DIR := src/
DOC_DIR := doc/
DOC_TYPE ?= doc
all:
nim compile --verbosity:0 --hints:on --showAllMismatches:on --out:$(BIN_DIR)multrix $(SRC_DIR)main.nim
@cp $(BIN_DIR)multrix $(BIN_DIR)dot
@cp $(BIN_DIR)multrix $(BIN_DIR)cross
doc:
nim $(DOC_TYPE) --outdir:$(DOC_DIR) $(SRC_DIR)main.nim
real: clean
nim compile --define:release --out:$(BIN_DIR)multrix $(SRC_DIR)main.nim
@cp $(BIN_DIR)multrix $(BIN_DIR)dot
@cp $(BIN_DIR)multrix $(BIN_DIR)cross
run: all
@echo "===================================================="
@echo ""
@$(BIN_DIR)multrix
cr: clean run
clean:
@rm -rf $(BIN_DIR) $(DOC_DIR)
clear: clean

22
PKGBUILD Normal file
View file

@ -0,0 +1,22 @@
# Maintainer: "Array in a Matrix" <tensor@arrayinamatrix.xyz>
pkgname=multrix
pkgver=1.3
pkgrel=1
pkgdesc="Matrix multiplication calculator."
arch=('x86_64')
url="https://git.inamatrix.xyz/array-in-a-matrix/multrix"
license=('GPL3')
makedepends=('nim' 'git')
# source=("$url/archive/$pkgname-$pkgver.tar.gz")
source=("$pkgname"::git+${url}.git)
sha512sums=('SKIP')
build() {
cd "$pkgname"
nimble build
}
package() {
cd "$pkgname"
install -Dm755 multrix $pkgdir/usr/bin/multrix
}

View file

@ -4,15 +4,23 @@ Matrix multiplication calculator; calculates the cross & dot products of matrice
## Usage
To calculate the dot product of 2 matrices execute the `dot` command or `multrix` command in the output `bin` directory.
To calculate the cross product of 2 vectors execute the `cross` command or `multrix` command in the output `bin` directory.
The `multrix` command can calculate both dot or cross products if given the respective command line argument. If no valid commandline argument is given the program will ask which type of calculation should be made.
## installation
The program requires no dependencies other than the nim standard library. Just run `make` in the root of the project and run the executables in `bin`.
To install using nimble:
```sh
nimble install https://git.inamatrix.xyz/array-in-a-matrix/multrix
```
To install using pacman:
```sh
git clone https://git.inamatrix.xyz/array-in-a-matrix/multrix
cd multrix
makepkg si
```
## Math
@ -48,7 +56,7 @@ $$
$$
\begin{align*}
\vec{a} \times \vec{b} =
\vec{A} \times \vec{B} =
\begin{vmatrix}
i & j & k\\
a_1 & a_2 & a_3\\

10
multrix.nimble Normal file
View file

@ -0,0 +1,10 @@
version = "1.3"
author = "Array in a Matrix"
description = "Matrix multiplication calculator."
license = "AGPL-3.0-or-later"
bin = @["multrix"]
srcDir = "src"
backend = "c"
requires "nim >= 2.0.2"

View file

@ -1,22 +0,0 @@
import strutils, os, procedures
var argument: string
if paramCount() > 0:
argument = toLowerAscii(paramStr(1))
let args = [paramStr(0).splitPath.tail, paramStr(0), argument]
if "dot" in args or "d" in args:
dot()
elif "cross" in args or "c" in args:
cross()
else:
echo "Would you like to preform the dot or cross product?"
case toLowerAscii(readLine(stdin)):
of "dot", "d":
dot()
of "cross", "c":
cross()
else:
quit "Invalid operation!", QuitFailure

27
src/multrix.nim Normal file
View file

@ -0,0 +1,27 @@
import strutils, os, procedures, terminal
var argument: string
if paramCount() > 0:
argument = toLowerAscii(paramStr(1))
let args = [paramStr(0).splitPath.tail, paramStr(0), argument]
if "dot" in args or "d" in args:
dot()
elif "cross" in args or "c" in args:
cross()
elif "scalar" in args or "s" in args:
scalar()
else:
styledEcho resetStyle, "Would you like to preform the ", styleBright, "dot", resetStyle, ", ", styleBright, "cross ", resetStyle, "or ", styleBright, "scalar ", resetStyle, "product?"
case toLowerAscii(readLine(stdin)):
of "dot", "d", "1":
dot()
of "cross", "c", "2":
cross()
of "scalar", "s", "3":
scalar()
else:
styledEcho fgRed, "Invalid operation!"
quit QuitFailure

View file

@ -1,4 +1,4 @@
import strutils, sequtils
import strutils, sequtils, terminal, re
#? validate if user input is of correct type
proc getInt: int =
@ -6,15 +6,23 @@ proc getInt: int =
try:
return parseInt(readline(stdin))
except:
echo "Please enter an integer, try again."
styledEcho resetStyle, "Please enter an ", styleBright, "integer, " , resetStyle, "try again."
#? validate if user input is of correct type
proc getFloat: float =
while(true):
try:
return parseFloat(readline(stdin))
let number: string = readline(stdin).replace(" ","").replace("\t", "")
#? if the number given is a fraction convert it into decimal
if match(number, re"[0-9]+/[0-9]+"):
let
numerator: float = number.split('/')[0].parseFloat
denominator: float = number.split('/')[1].parseFloat
return numerator / denominator
return parseFloat(number)
except:
echo "Please enter a number, try again."
styledEcho resetStyle, "Please enter a ", styleBright, "number, " , resetStyle, "try again."
#? prints a matrix to the standard output
proc printMatrix*(matrix: seq[seq[float]]) =
@ -31,6 +39,20 @@ proc fillMatrix*(matrix: var seq[seq[float]], row, col: int) =
matrix[i-1].delete(0)
echo matrix[i-1]
#? calculate scalar product
proc calcScalar(factor: float, matrix: seq[seq[float]]): seq[seq[float]] =
let col: int = matrix[0].len
let row: int = matrix.len
var newMatrix = newSeqWith(row, newSeq[float](col))
if col == row and col == 1:
styledEcho fgRed, "\nPure scalar multiplication detected!"
return @[@[ factor * matrix[0][0]]]
for i in countup(0, row-1):
for j in countup(0, col-1):
newMatrix[i][j] = factor * matrix[i][j]
return newMatrix
#? calculate dot product
proc calcDot(matrix1: seq[seq[float]], matrix2: seq[seq[float]]): seq[seq[float]] =
let col1: int = matrix1[0].len
@ -39,11 +61,31 @@ proc calcDot(matrix1: seq[seq[float]], matrix2: seq[seq[float]]): seq[seq[float]
let row2: int = matrix2.len
var col, row: int
if col1 == row2:
#? check if both matrics are actually scalars
if (col1 == row1 and col1 == 1) and (col2 == row2 and col2 == 1):
styledEcho fgRed, "\nPure scalar multiplication detected!"
return @[@[matrix1[0][0] * matrix2[0][0]]]
#? check if the first matrix is actually a scalar
elif col1 == row1 and col1 == 1:
styledEcho fgRed, "\nMatrix scalar multiplication detected!"
var matrix = newSeqWith(row2, newSeq[float](col2))
matrix = calcScalar(matrix1[0][0], matrix2)
return matrix
#? check if the second matrix is actually a scalar
elif col2 == row2 and col2 == 1:
styledEcho fgRed, "\nMatrix scalar multiplication detected!"
var matrix = newSeqWith(row1, newSeq[float](col1))
matrix = calcScalar(matrix2[0][0], matrix1)
return matrix
elif col1 == row2:
col = col2
row = row1
else:
quit "Matrix dimensions mismatched, operation invalid!", QuitFailure
styledEcho fgRed, "Matrix dimensions mismatched!"
quit QuitFailure
var matrix = newSeqWith(row, newSeq[float](col))
@ -61,29 +103,28 @@ proc calcCross(vector1: array[3, float], vector2: array[3, float]): array[3, flo
result = [i, j, k]
proc dot* =
echo "MATRIX DOT PRODUCT"
styledEcho styleBright, "Matrix Dot Product"
#? record first matrix
echo "Enter number of rows in the first matrix:"
styledEcho "Enter number of ", styleBright, "rows ", resetStyle, "in the first matrix:"
let r1: int = getInt()
echo "Enter number of columns in the first matrix:"
styledEcho "Enter number of ", styleBright, "columns ", resetStyle, "in the first matrix:"
let c1: int = getInt()
var m1 = newSeqWith(r1, newSeq[float](c1))
procedures.fillMatrix(m1, r1, c1)
fillMatrix(m1, r1, c1)
echo ""
#? record second matrix
echo "Enter number of rows in the second matrix:"
styledEcho "Enter number of ", styleBright, "rows ", resetStyle, "in the second matrix:"
let r2: int = getInt()
echo "Enter number of columns in the second matrix:"
styledEcho "Enter number of ", styleBright, "columns ", resetStyle, "in the second matrix:"
let c2: int = getInt()
var m2 = newSeqWith(r2, newSeq[float](c2))
procedures.fillMatrix(m2, r2, c2)
fillMatrix(m2, r2, c2)
#? resultent matrix
var m: seq[seq[float]]
m = calcDot(m1, m2)
let m: seq[seq[float]] = calcDot(m1, m2)
echo "\nFirst matrix is:"
printMatrix(m1)
@ -93,27 +134,48 @@ proc dot* =
printMatrix(m)
proc cross* =
echo "VECTOR CROSS PRODUCT"
styledEcho styleBright, "Vector Cross Product"
type
VECTOR = array[3, float]
var
v1: VECTOR
v2: VECTOR
v: VECTOR
echo "Enter numbers in the first vector:"
styledEcho "Enter numbers in the ", styleBright, "first ", resetStyle, "vector:"
for i in 0..2:
echo "Enter item:"
v1[i] = getFloat()
echo "Enter numbers in the second vector:"
styledEcho "Enter numbers in the ", styleBright, "second ", resetStyle, "vector:"
for i in 0..2:
echo "Enter item:"
v2[i] = getFloat()
#? resultent vector
let v = calcCross(v1, v2)
v = calcCross(v1, v2)
echo v1, " \u2A2F ", v2
echo "\nResult vector is:"
echo v
proc scalar* =
styledEcho styleBright, "Matrix Scalar Multiplication"
styledEcho "Enter the " ,styleBright, "scale factor", resetStyle, ":"
let f: float = getFloat()
styledEcho "Enter number of ", styleBright, "rows ", resetStyle, "in the matrix:"
let r: int = getInt()
styledEcho "Enter number of ", styleBright, "columns ", resetStyle, "in the matrix:"
let c: int = getInt()
var m = newSeqWith(r, newSeq[float](c))
fillMatrix(m, r, c)
let M = calcScalar(f, m)
echo "\nInitial matrix is:"
printMatrix(m)
echo "\nScaled matrix is:"
printMatrix(M)