-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix
343 lines (292 loc) · 10.9 KB
/
matrix
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
#****************************************************************************************************
# ** Author: WeiHao Kuang
# ** Email: kuangw@oregonstate.edu
# ** Description: This script is a matrix calculator and will perfrom simple mmatrix operations
# dims, transpose, mean, addition, and multiplication
#****************************************************************************************************
# References used:
# Canvas Assignment 1 Hints Section
# https://ss64.com/bash/wc.html
# https://www.cyberciti.biz/faq/bash-for-loop/
# https://ss64.com/bash/paste.html
# https://stackoverflow.com/questions/2439579/how-to-get-the-first-line-of-a-file-in-a-bash-script
# https://www.cyberciti.biz/faq/unix-for-loop-1-to-10/
# https://unix.stackexchange.com/questions/26601/how-to-read-from-two-input-files-using-while-loop
# this the trap that will remove files that are temp and are not useful after the program runs
# also this is used to delete the temp files in case there are program interupts, hangs, crashes etc.
trap "rm -f datafile* d2utmp* mtest* add* mdank* dank*;" QUIT KILL HUP INT TERM EXIT
# NAME:
# dims - gets the dimension for a matrix that is passed in
# SYNOPSIS:
# dims matrix
# DESCRIPTION:
# Prints "ROWS COLS" (Space separated!)
function dims () {
datafilepath="datafile$$" #variable holding tempfile path
#handles input from stdin when there is no args with dims
if [ $# = 0 ]
then
if [ ! -t 0 ] # if stdin not empty
then
cat > "$datafilepath"
else #if stdin is empty
printf "%s\n" "Incorrect # or args" 1>&2
exit 1
fi
elif [ "$#" = 1 ] #if there is 1 arg entered with dims
then
datafilepath=$1 #datafilepath will get first argument
fi
#if argument count greater than 1, then error to stderr
if [ $# -gt 1 ]
then
echo "Error only one statement can be used!" 1>&2
exit 1
#if argument file is not readable error statment to stderr
elif [ ! -r $1 ]
then
echo "Not readable file!" 1>&2
exit 1
fi
#row counting using cat and wc command on argument file passed in
rows=$(cat $datafilepath | wc -l)
# uses the head command and row specifer to get
# the columns of the in the line
cols=$( head -n 1 $datafilepath | wc -w)
echo -e "$rows $cols"
}
# NAME:
# transpose - transposes the matrix that is passed in
# SYNOPSIS:
# transpose matrix
# DESCRIPTION:
# Prints the transpose of the input, in a valid matrix format
function transpose () {
if [ $# = 0 ]
then
if [ ! -t 0 ] # if stdin not empty
then
cat > "$datafilepath"
else #if stdin is empty
printf "%s\n" "Incorrect # or args" 1>&2
exit 1
fi
elif [ "$#" = 1 ] #if there is 1 arg entered with dims
then
datafilepath=$1 #datafilepath will get first argument
fi
#checks to see if the arg count is > 1
#if true then exit 1 with error statment to stderr
if [ $# -gt 1 ]
then
echo "Incorrect arguments!" 1>&2
exit 1
#checks to see of the file passed in is readable
#if not then exit 1 status, and error message to stderr
elif [ ! -r $1 ]
then
echo "File not readable!" 1>&2
exit 1
#runs the transpose on the passed in file
else
#gets the cols for passed in matrix
cols=$( head -n 1 $datafilepath | wc -w)
#for loop running cols amount of times
for ((i=1;i<=$cols;i++));
do
#cut is used to get the column
#paste -s is used to get the column into a row
cut -f$i $datafilepath | paste -s
done
fi
}
# NAME:
# mean - find the mean value of the matrix file passed in
# SYNOPSIS:
# mean matrix
# DESCRIPTION:
# Prints a row vector mean of the input matrix, matrix
# format with ***.5 values rounded away from zero
function mean () {
if [ $# = 0 ]
then
if [ ! -t 0 ] # if stdin not empty
then
cat > "$datafilepath"
else #if stdin is empty
printf "%s\n" "Incorrect # or args" 1>&2
exit 1
fi
elif [ "$#" = 1 ] #if there is 1 arg entered with dims
then
datafilepath=$1 #datafilepath will get first argument
fi
mtest="mtest$$" #process id in name for tempfile var
if [ ! -r $datafilepath ] #if file is not readable
then
echo "File not readable!" 1>&2
exit 1
elif [ $# -gt 1 ] #if the argument count is > 1
then
echo "Incorrect arguments!" 1>&2
exit 1
else
rows=$(cat $datafilepath | wc -l) #rows of the matrix
cols=$(head -n 1 $datafilepath | wc -w) #cols of the matrix
# for loop iterates the amount of cols in matrix
for ((i=1;i<=$cols;i++))
do
cut -f$i $datafilepath > $mtest # cutted col goes to tempfile
sum=0
#while loop is used to read the tempfile line by line
while read line
do
sum=$(( $line+$sum )) # gets the sum of the column
# this will get the average of the column
average=$(( ($sum + ($rows/2)*( ($sum>0)*2-1 )) / $rows ))
done < $mtest
if [ $i -eq $cols ] # this will print the last cols with newline chars
then
printf "%s\n" $average
else # otherwise that number that is printed will be tab delimited
printf "%s\t" $average
fi
done
fi
}
# NAME:
# add - finds the added matrix from 2 input matrices
# SYNOPSIS:
# mean matrix1 matrix2
# DESCRIPTION:
# prints the sum, in a valid matrix format to stdout
function add () {
#tempfiles with process ids in their names
add_file1="add_file1$$"
add_file2="add_file2$$"
mtest1="mtest1$$"
if [ $# -ne 2 ] # if the arg count ne 2
then
printf "%s\n" "Incorrect arguments!" 1>&2
exit 1
elif [ ! -r $1 ] || [ ! -r $2 ] # if the either file is not readable
then
printf "%s\n" "File/s not readable!" 1>&2
exit 1
else
#obtaining the row and col infomation of the matrices m1 and m2
rows1=$(cat $1 | wc -l) # cat to get rows1
rows2=$(cat $2 | wc -l) # cat to get rows2
cols1=$(head -n 1 $1 | wc -w) # using word count to get cols1
cols2=$(head -n 1 $2 | wc -w) # using word count to get cols1
# conditional to check the adding dimensions
if [ $rows1 != $rows2 ] || [ $cols1 != $cols2 ]
then
printf "%s\n" "Matrix dimensions don't match!" 1>&2
exit 1
else
# for loop runs cols1 times
for ((i=1;i<=$cols1;i++))
do
# cutting the cols from m1 and m2
# iterated by the for loop cols times
cut -f$i $1 > $add_file1
cut -f$i $2 > $add_file2
counter=1
# simultaneously reading bother add1 and add2
while read theline1 <&3; read theline2 <&4;
do
#adding the two number together from the column files
addition=$(( $theline1+$theline2 ))
#conditions are used to place the sums inside of a tempfile
if [ $counter = $rows1 ] #when the counter and rows are the same
then
printf "%d%s\n" $addition >> $mtest1 # append to the tempfile
else
printf "%d%s\t" $addition >> $mtest1 # append to the tempfile
fi
((counter++))
done 3<$add_file1 4<$add_file2 # reading from tempfiles add_file1 and 2
done
# condionals below are used to determine the loop iterations
# in the for loop in line 215
iter=0
if [ $cols1 -ne $rows1 ]
then
iter=$rows1 # titeration variable
else
iter=$cols1 # iteration variable
fi
# this for loop is used to display the summation matrix from the tempfiles
for ((i=1;i<=$iter;i++));
do
cut -f$i $mtest1 | paste -s # cut and paste to get to right format
done
fi
fi
}
# NAME:
# multiply - finds the product matrix from 2 input matrices
# SYNOPSIS:
# multiply matrix1 matrix2
# DESCRIPTION:
# Prints a product matrix that is correct to the stdout
function multiply () {
# tempfile with their process ids in the naming
mdank="mdank$$"
dankfile="dankfile$$"
dankmemes="dankmemes$$"
if [ $# -ne 2 ] # if the arg count not equal to 2
then
printf "%s\n" "Incorrect arguments!"
elif [ ! -r $1 ] || [ ! -r $2 ] # if either file is not readable
then
printf "%s\n" "Incorrect arguments!"
else
#obtaining column and row information from m1 and m2
rows1=$(cat $1 | wc -l) # cat to get rows1
rows2=$(cat $2 | wc -l) # cat to get rows2
cols1=$(head -n 1 $1 | wc -w) # using word count to get cols1
cols2=$(head -n 1 $2 | wc -w) # using word count to get cols2
# conditional checks to see if the matrix dimensions are compatible
# such that: NxP * PxN
if [ $cols1 -ne $rows2 ]
then
echo "Broken" 1>&2
exit 1
else # run the multipling code
transpose $1 > $mdank #transposing the first inputted matrix
#obtain column information from the transposed first matrix
T_cols=$(head -n 1 $mdank | wc -w)
# outter for loop used to iterate through the transposed matrix
# columns
for((i=1;i<=$T_cols;i++))
do
cut -f$i $mdank > $dankfile # cutting the transposed matrix into temp
# 2nd for loop is used to iterate through the cols of the 2 matrix
for((j=1;j<=$cols2;j++))
do
sum_oproducts=0
cut -f$j $2 > $dankmemes # cutting cols from 2nd matrix
# while loop is used to read temp file simulatneously
#whilst multipling and adding them together
while read theline1 <&3; read theline2 <&4;
do
product_ans=$(( $theline1*$theline2 )) # multply rows and cols
# adding the products together
sum_oproducts=$(( $product_ans+$sum_oproducts ))
done 3<$dankfile 4<$dankmemes
# condionals used to print out the matrix from var sum_products
if [ $j -eq $cols2 ]
then
printf "%s\n" $sum_oproducts
else
printf "%s\t" $sum_oproducts
fi
done
done
fi
fi
}
$1 "${@:2}"