Advertisement
Guest User

Matrix Operations

a guest
Apr 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##########Intro###############
  4. #So for this program I tried desperately to get it to work with the grading script.
  5. #Sadly I was unsuccessful, I will rewatch the stdout videos and see where I went wrong.
  6. #However, I took an approach of using arrays to calculate the matrixes after studying up
  7. #on how they work in bash. I tested my program mainly using files to ensure the core functionality
  8. #worked. I outputted my work using printf so that I could use \t as tab instead of echo as it was
  9. #not working how I wanted it to. However, since I was using printf which defaults to stdout I thought
  10. #that it would work without too much trouble. Apparently I was wrong, so I spent a lot of my time
  11. #trying to understand why the grading script wasn't seeing the output correctly. Overall, my program
  12. #works and is able to go through matrixes quite quickly and give the right outputs. Sadly I will
  13. #get a zero on this since I wasn't able to correctly output the information.
  14. ##########Dims####################
  15.  
  16. Dim(){
  17.    #Declare an array to store the items of the first line
  18.    #I then use this to find the number of columns by length
  19.    #of the array.
  20.    declare -a array=()
  21.    read -a line < "$inputFile"
  22.  
  23.    COLS=${#line[@]}
  24.    ROWS=0
  25.  
  26.    #Iterate through the lines until eof
  27.    while read -a line
  28.    do
  29.         ((ROWS++))
  30.    done < "$inputFile"
  31.  
  32.    printf "%s %s\n" ${ROWS} ${COLS}
  33.    exit 0
  34. }
  35.  
  36. ##########Transpose##############
  37.  
  38. Transpose(){
  39.     #Declare array and set columns
  40.     declare -a array=()
  41.     read -a line < "$inputFile"
  42.  
  43.     COLS=${#line[@]}
  44.  
  45.     #iterate through the file and place in 1d array
  46.     #I decided that this would be the easiest way to
  47.     #capture the rows and also be able to work immediately
  48.     #with the array.
  49.     index=0
  50.     while read -a line
  51.     do
  52.         for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++ ))
  53.         do
  54.             array[$index]=${line[$COUNTER]}
  55.             ((index++))
  56.         done
  57.     done < "$inputFile"
  58.  
  59.     #Using a double for loop we place each element in the corresponding
  60.     #column before moving to the next row. Conditionals were placed to
  61.     #see if the grading script would accept the formatted output. However,
  62.     #it became bloated and it wasn't helping so I removed them.
  63.     for (( ROW = 0; ROW < COLS; ROW++ ))
  64.     do
  65.         for (( COUNTER = ROW; COUNTER < ${#array[@]}; COUNTER += COLS ))
  66.         do
  67.             printf "%s\t" ${array[$COUNTER]}
  68.         done
  69.         printf "\n"
  70.     done
  71.     exit 0
  72. }
  73.  
  74. ###########Mean#################
  75.  
  76. Mean(){
  77.     #Declare our initial variables, arrays, ect...
  78.     declare -a array=()
  79.  
  80.     read -a line < "$inputFile"
  81.  
  82.     COLS=${#line[@]}
  83.     ROWS=0
  84.  
  85.     tempA=0
  86.     tempB=0
  87.  
  88.     #From here we add to the respective column in the array, so elements
  89.     #in column one are added to array[0] and so forth.
  90.     while read -a line
  91.     do
  92.         for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++ ))
  93.         do
  94.             tempA=${array[$COUNTER]}
  95.             tempB=${line[$COUNTER]}
  96.             array[$COUNTER]=$((tempA + tempB))
  97.         done
  98.         ((ROWS++))
  99.     done < "$inputFile"
  100.    
  101.     #Depending on the number of rows we divide the elements,
  102.     #and if the sum is negative round with a negative 1.
  103.     for(( COUNTER=0; COUNTER<COLS; COUNTER++ ))
  104.     do
  105.         tempA=${array[$COUNTER]}
  106.  
  107.         if [ "$tempA" -gt "0" ]
  108.         then
  109.            tempA=$(( (tempA+(ROWS/2))/ROWS ))
  110.         else
  111.            tempA=$(( (tempA+(ROWS/2)*(0-1))/ROWS ))
  112.         fi
  113.        
  114.         array[$COUNTER]=$((tempA))
  115.         printf "%s\t" ${array[$COUNTER]}
  116.     done
  117.     printf "\n"
  118. }
  119.  
  120. ###########Add##################
  121.  
  122. Add(){
  123.     #Declare the arrays that we need to add to
  124.     declare -a arrayA=()
  125.     declare -a arrayB=()
  126.     declare -a arrayC=()
  127.     arrayC[0]=0
  128.  
  129.     read -a line < "$inputFile"
  130.  
  131.     COLSA=${#line[@]}
  132.     ROWSA=0
  133.  
  134.     index=0
  135.     #We read from the two files into their respective arrays A or B
  136.     while read -a line
  137.     do
  138.         for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
  139.         do
  140.             arrayA[$index]=${line[$COUNTER]}
  141.             ((index++))
  142.         done
  143.         ((ROWSA++))
  144.     done < "$inputFile"
  145.  
  146.     read -a line < "$modFile"
  147.  
  148.     COLSB=${#line[@]}
  149.     ROWSB=0
  150.     index=0
  151.    
  152.     while read -a line
  153.     do
  154.         for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
  155.         do
  156.             arrayB[$index]=${line[$COUNTER]}
  157.             ((index++))
  158.         done
  159.         ((ROWSB++))
  160.     done < "$modFile"
  161.  
  162.     #End of reading into the arrays.
  163.  
  164.     #Here we check to see if the arrays are valid, I should do this check initially
  165.     #but since my method required me to read the arrays first. I decided to do it
  166.     #afterwards. Pretty simple, add each element from both arrays into a new array.
  167.     tempA=0
  168.     tempB=0
  169.    
  170.     if [[ "$ROWSB" == "$ROWSA" && "$COLSA" == "$COLSB" ]]
  171.     then
  172.         for(( COUNTER=0; COUNTER<index; COUNTER++))
  173.         do
  174.             tempA=${arrayA[$COUNTER]}
  175.             tempB=${arrayB[$COUNTER]}
  176.             arrayC[$COUNTER]=$(( tempA+tempB ))
  177.         done
  178.  
  179.         index=0
  180.  
  181.         for(( ROW=0; ROW<ROWSA; ROW++ ))
  182.         do
  183.             for(( COL=0; COL<COLSA; COL++ ))
  184.             do
  185.                 printf "%s\t" ${arrayC[$index]}
  186.                 ((index++))
  187.             done
  188.             printf "\n"
  189.         done
  190.     else
  191.         echo "Incompatible" >&2
  192.         exit 1
  193.     fi
  194. }
  195.  
  196. ###########Multiply###############
  197.  
  198. Multiply(){
  199.     #Declare arrays for multiplication
  200.     declare -a arrayA=()
  201.     declare -a arrayB=()
  202.     declare -a arrayC=()
  203.  
  204.     read -a line < "$inputFile"
  205.  
  206.     COLSA=${#line[@]}
  207.     ROWSA=0
  208.  
  209.     index=0
  210.     #ANOTHER READ BLOCK, so exciting
  211.     while read -a line
  212.     do
  213.         for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
  214.         do
  215.             arrayA[$index]=${line[$COUNTER]}
  216.             ((index++))
  217.         done
  218.         ((ROWSA++))
  219.     done < "$inputFile"
  220.  
  221.     read -a line < "$modFile"
  222.  
  223.     COLSB=${#line[@]}
  224.     ROWSB=0
  225.     index=0
  226.  
  227.     while read -a line
  228.     do
  229.         for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
  230.         do
  231.             arrayB[$index]=${line[$COUNTER]}
  232.             ((index++))
  233.         done
  234.         ((ROWSB++))
  235.     done < "$modFile"
  236.  
  237.     #END OF READ BLOCK.... Yay! Have cols and rows of both arrays
  238.  
  239.     #Tests to see if the NxM MxP if the M are equal to each other.
  240.     #If they are, multiply the correct elements to each other.
  241.     #Again the check is done after loading the arrays, due to my
  242.     #method of iterating through the files.
  243.     tempA=0
  244.     tempB=0
  245.     index=0
  246.     if [ "$COLSA" == "$ROWSB" ]
  247.     then
  248.         for(( COUNTERA=0; COUNTERA<ROWSA; COUNTERA++ ))
  249.         do
  250.             for(( COUNTERB=0; COUNTERB<COLSB; COUNTERB++ ))
  251.             do
  252.                 temp=0
  253.                 for(( COUNTERC=0; COUNTERC<ROWSB; COUNTERC++ ))
  254.                 do
  255.                     tempA=${arrayA[$COUNTERA*$COLSA+$COUNTERC]}
  256.                     tempB=${arrayB[$COUNTERC*$COLSB+$COUNTERB]}
  257.                     temp=$(( temp+tempA*tempB ))
  258.                 done
  259.                 arrayC[$index]=$temp
  260.                 ((index++))
  261.             done
  262.         done
  263.         index=0
  264.         for(( ROW=0; ROW<ROWSA; ROW++ ))
  265.         do
  266.             for(( COL=0; COL<COLSB; COL++ ))
  267.             do
  268.                 printf "%s\t" ${arrayC[$index]}
  269.                 ((index++))
  270.             done
  271.             printf "\n"
  272.         done
  273.     else
  274.         echo "Incompatible" >&2
  275.         exit 1
  276.     fi
  277. }
  278.  
  279. ###########Main Code##############
  280. #Initial check to make sure no arguements exceed the max of 3
  281. if [ "$#" -gt "3" ]
  282. then
  283.     echo "Invalid number of parameters" >&2
  284.     exit 1
  285. fi
  286.  
  287. inputFile="$2"
  288. modFile="$3"
  289.  
  290. #Tests to see if inputFile is valid, should put another for modfile
  291. #but it won't matter to the gradingscript... Oh well make it work
  292. #and not break it. Also made a check to see if args were 1
  293. #That way I could pipe stdin to a tmp file and then inputfile
  294. #would receive that file. However, I did not give myself enough
  295. #time to finish this.
  296. if test -e "$inputFile" -a -r "$inputFile" #-o "$#" -eq 1
  297. then
  298.  
  299. case "$1" in
  300.    add)
  301.       #echo "add mode active"
  302.       if test -e "$modFile" -a -r "$modFile" -a "$#" -ne "3"
  303.       then
  304.         echo "Invalid number of parameters" >&2
  305.         exit 1
  306.       fi
  307.       Add
  308.       ;;
  309.  
  310.    dims)
  311.       #echo "attempting dims mode"
  312.       if [ "$#" -gt "2" ]
  313.       then
  314.         echo "Invalid number of parameters" >&2
  315.         exit 1
  316.       fi
  317.       Dim
  318.       ;;
  319.  
  320.    mean)
  321.       #echo "mean mode active"
  322.       if [ "$#" -ne "3" ]
  323.       then
  324.         echo "Invalid number of parameters" >&2
  325.         exit 1
  326.       fi
  327.       Mean
  328.       ;;
  329.  
  330.    multiply)
  331.       #echo "multiply mode active"
  332.       if test -e "$modFile" -a -r "$modFile" -a "$#" -ne "3"
  333.       then
  334.         echo "Invalid number of parameters" >&2
  335.         exit 1
  336.       fi
  337.       Multiply
  338.       ;;
  339.  
  340.    transpose)
  341.       #echo "transpose mode active"
  342.       if [ "$#" -gt "2" ]
  343.       then
  344.         echo "Invalid number of parameters" >&2
  345.         exit 1
  346.       fi
  347.       Transpose
  348.       ;;
  349.  
  350.    *)
  351.       echo $"Usage: $0 {add|dims|mean|multiply|transpose}">&2
  352.       exit 1
  353. esac
  354.  
  355. else
  356.     echo "Invalid File" >&2
  357.     exit 1
  358. fi
  359. exit 0
  360. #IamterribleatBASH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement