This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
vec.sh
81 lines (79 loc) · 1.91 KB
/
vec.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! /bin/bash
function vAssert() {
v=$(eval echo "\$\{$1[@]\}")
all=$(eval echo "$v")
v=$(eval echo "\$\{$1[0]\}")
name=$(eval echo "$v")
v=$(eval echo "\$\{#$1[@]\}")
len=$(eval echo "$v")
if [ "${name}" != "VEC" ]; then
echo \( ${all} \)is not Vector: $name
exit -1
fi
if [ $len -ne 5 ]; then
echo \( ${all} \) not Vector: length: $len
exit -1
fi
}
function vAdd {
in1=($(eval echo $(eval echo "\$\{$1[@]\}")))
in2=($(eval echo $(eval echo "\$\{$2[@]\}")))
vAssert in1
vAssert in2
eval <<- EOF $3=\(VEC \
\$\(expr ${in1[1]} \\\+ ${in2[1]} \) \
\$\(expr ${in1[2]} \\\+ ${in2[2]} \) \
\$\(expr ${in1[3]} \\\+ ${in2[3]} \) \
\$\(expr ${in1[4]} \\\+ ${in2[4]} \) \
\)
EOF
}
function vSub {
in1=($(eval echo $(eval echo "\$\{$1[@]\}")))
in2=($(eval echo $(eval echo "\$\{$2[@]\}")))
vAssert in1
vAssert in2
eval <<- EOF $3=\(VEC \
\$\(expr ${in1[1]} - ${in2[1]} \) \
\$\(expr ${in1[2]} - ${in2[2]} \) \
\$\(expr ${in1[3]} - ${in2[3]} \) \
\$\(expr ${in1[4]} - ${in2[4]} \) \
\)
EOF
}
function vDiv {
in1=($(eval echo $(eval echo "\$\{$1[@]\}")))
in2=$2
vAssert in1
eval <<- EOF $3=\(VEC \
\$\(expr ${in1[1]} \\\* $FACTOR \\\/ ${in2} \) \
\$\(expr ${in1[2]} \\\* $FACTOR \\\/ ${in2} \) \
\$\(expr ${in1[3]} \\\* $FACTOR \\\/ ${in2} \) \
\$\(expr ${in1[4]} \\\* $FACTOR \\\/ ${in2} \) \
\)
EOF
}
function vMul {
in1=($(eval echo $(eval echo "\$\{$1[@]\}")))
in2=$2
vAssert in1
eval <<- EOF $3=\(VEC \
\$\(expr ${in1[1]} \\\* ${in2} \\\/ $FACTOR \) \
\$\(expr ${in1[2]} \\\* ${in2} \\\/ $FACTOR \) \
\$\(expr ${in1[3]} \\\* ${in2} \\\/ $FACTOR \) \
\$\(expr ${in1[4]} \\\* ${in2} \\\/ $FACTOR \) \
\)
EOF
}
function vDot {
in1=($(eval echo $(eval echo "\$\{$1[@]\}")))
in2=($(eval echo $(eval echo "\$\{$2[@]\}")))
vAssert in1
vAssert in2
eval <<- EOF $3=\$\(expr \\\( \
${in1[1]} \\\* ${in2[1]} \\\+ \
${in1[2]} \\\* ${in2[2]} \\\+ \
${in1[3]} \\\* ${in2[3]} \\\+ \
${in1[4]} \\\* ${in2[4]} \\\) \\\/ $FACTOR \)
EOF
}