|
| 1 | +# importing Numpy package |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +#Makes sure decimal numbers are entered |
| 6 | +def trial_and_error(rowCol): |
| 7 | + try: |
| 8 | + row_col_input = float(input()) |
| 9 | + return row_col_input |
| 10 | + except ValueError: |
| 11 | + print('Please only input digits') |
| 12 | + |
| 13 | +class array_from_user: # main matrix class |
| 14 | + |
| 15 | + loopControl = 0 #counter throughout code |
| 16 | + #variables for 3x3 matrix |
| 17 | + Row1Col1 = 0 |
| 18 | + Row1Col2 = 0 |
| 19 | + Row1Col3 = 0 |
| 20 | + |
| 21 | + Row2Col1 = 0 |
| 22 | + Row2Col2 = 0 |
| 23 | + Row2Col3 = 0 |
| 24 | + |
| 25 | + Row3Col1 = 0 |
| 26 | + Row3Col2 = 0 |
| 27 | + Row3Col3 = 0 |
| 28 | + |
| 29 | + #variables for second matrix of knowns |
| 30 | + |
| 31 | + row1_col1_second = 0 |
| 32 | + row2_col1_second = 0 |
| 33 | + row3_col1_second = 0 |
| 34 | + |
| 35 | + detOriginal = 0 |
| 36 | + det_delta_l = 0 |
| 37 | + det_delta_2 = 0 |
| 38 | + |
| 39 | + det_delta_3 = 0 |
| 40 | + |
| 41 | + original_array = np.array([[0, 0, 0],[0, 0, 0],[0, 0, 0]]) |
| 42 | + delta1_array = np.array([[0, 0, 0],[0, 0, 0],[0, 0, 0]]) |
| 43 | + delta2_array = np.array([[0, 0, 0],[0, 0, 0],[0, 0, 0]]) |
| 44 | + delta3_array = np.array([[0, 0, 0],[0, 0, 0],[0, 0, 0]]) |
| 45 | + #__init__ is a constructor or start up class that will use ArraySelf as an instance |
| 46 | + |
| 47 | + #specification throughout start up |
| 48 | + def __init__(ArraySelf, row1_col1, row1_col2, row1_col3, row2_col1, |
| 49 | + row2_col2, row2_col3, row3_col1, row3_col2, row3_col3, second_1, second_2, second_3 ): |
| 50 | +#All assignments of values |
| 51 | + ArraySelf.Row1Col1 = row1_col1 |
| 52 | + ArraySelf.Row1Col2 = row1_col2 |
| 53 | + ArraySelf.Row1Col3 = row1_col3 |
| 54 | + |
| 55 | + ArraySelf.Row2Col1 = row2_col1 |
| 56 | + ArraySelf.Row2Col2 = row2_col2 |
| 57 | + ArraySelf.Row2Col3 = row2_col3 |
| 58 | + |
| 59 | + ArraySelf.Row3Col1 = row3_col1 |
| 60 | + ArraySelf.Row3Col2 = row3_col2 |
| 61 | + ArraySelf.Row3Col3 = row3_col3 |
| 62 | + |
| 63 | + ArraySelf.row1_col1_second = second_1 |
| 64 | + ArraySelf.row2_col1_second = second_2 |
| 65 | + ArraySelf.row3_col1_second = second_3 |
| 66 | + |
| 67 | + #Displayable Matrix results |
| 68 | + ArraySelf.original_array = np.array([[ArraySelf.Row1Col1, ArraySelf.Row1Col2, ArraySelf.Row1Col3], |
| 69 | + [ArraySelf.Row2Col1, ArraySelf.Row2Col2, ArraySelf.Row2Col3], |
| 70 | + [ArraySelf.Row3Col1, ArraySelf.Row3Col2, ArraySelf.Row3Col3]]) |
| 71 | + |
| 72 | + ArraySelf.delta1_array = np.array([[ArraySelf.row1_col1_second, ArraySelf.Row1Col2, ArraySelf.Row1Col3], |
| 73 | + [ArraySelf.row2_col1_second, ArraySelf.Row2Col2, ArraySelf.Row2Col3], |
| 74 | + [ArraySelf.row3_col1_second, ArraySelf.Row3Col2, ArraySelf.Row3Col3]]) |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + ArraySelf.delta2_array = np.array([[ArraySelf.Row1Col1, ArraySelf.row1_col1_second, ArraySelf.Row1Col3], |
| 79 | + |
| 80 | + [ArraySelf.Row2Col1, ArraySelf.row2_col1_second, ArraySelf.Row2Col3], |
| 81 | + |
| 82 | + [ArraySelf.Row3Col1, ArraySelf.row3_col1_second, ArraySelf.Row3Col3]]) |
| 83 | + |
| 84 | + |
| 85 | + ArraySelf.delta3_array = np.array([[ArraySelf.Row1Col1, ArraySelf.Row1Col2, ArraySelf.row1_col1_second], |
| 86 | + [ArraySelf.Row2Col1, ArraySelf.Row2Col2, ArraySelf.row2_col1_second], |
| 87 | + [ArraySelf.Row3Col1, ArraySelf.Row3Col2, ArraySelf.row3_col1_second]]) |
| 88 | + #Determinants solved |
| 89 | + ArraySelf.detOriginal = np.linalg.det(ArraySelf.original_array) |
| 90 | + ArraySelf.det_delta_l = np.linalg.det(ArraySelf.delta1_array) |
| 91 | + ArraySelf.det_delta_2 = np.linalg.det(ArraySelf.delta2_array) |
| 92 | + ArraySelf.det_delta_3 = np.linalg.det(ArraySelf.delta3_array) |
| 93 | + |
| 94 | + |
| 95 | +#Print all values to the user |
| 96 | + print('Cramers Law step by step\nOriginal Array:\n') |
| 97 | + print(ArraySelf.original_array) |
| 98 | + print('Determinant:') |
| 99 | + print(ArraySelf.detOriginal) |
| 100 | + |
| 101 | + print('\nDelta 1:\n ') |
| 102 | + print(ArraySelf.delta1_array) |
| 103 | + print('Determinant:') |
| 104 | + print(ArraySelf.det_delta_l) |
| 105 | + |
| 106 | + print('\nDelta 2:\n ') |
| 107 | + print(ArraySelf.delta2_array) |
| 108 | + print('Determinant:') |
| 109 | + print(ArraySelf.det_delta_2) |
| 110 | + |
| 111 | + print('\nDelta 3:\n ') |
| 112 | + print(ArraySelf.delta3_array) |
| 113 | + print('Determinant:') |
| 114 | + print(ArraySelf.det_delta_3) |
| 115 | + |
| 116 | + print('\nUnknown variable 1 delta 1 / original ') |
| 117 | + print(ArraySelf.det_delta_l / ArraySelf.detOriginal) |
| 118 | + |
| 119 | + print('\nUnknown variable 2 delta 2 / original ') |
| 120 | + print(ArraySelf.det_delta_2 / ArraySelf.detOriginal) |
| 121 | + |
| 122 | + print('\nUnknown variable 3 delta 3 / original ') |
| 123 | + print(ArraySelf.det_delta_3 / ArraySelf.detOriginal) |
| 124 | + |
| 125 | + #Used to set up original matrix and delta variants so that we |
| 126 | + #can divide the determinants in the end. |
| 127 | + #calls to reset the numbers in array |
| 128 | + |
| 129 | + def setOriginal(ArraySelf): |
| 130 | + ArraySelf.original_array = np.array([[Row1Col1, Row1Col2, Row1Col3], |
| 131 | + |
| 132 | + [Row2Col1, Row2Col2, Row2Col3], |
| 133 | + |
| 134 | + [Row3Col1, Row3Col2, Row3Col3]]) |
| 135 | + |
| 136 | + def setDelta1(): |
| 137 | + ArraySelf.delta1_array = np.array([[row1_col1_second, Row1Col2, Row1Col3], |
| 138 | + [row2_col1_second, Row2Col2, Row2Col3], |
| 139 | + [row3_col1_second, Row3Col2, Row3Col3]]) |
| 140 | + |
| 141 | + def setDelta2(): |
| 142 | + delta2_array = np.array([[Row1Col1, row1_col1_second, Row1Col3], |
| 143 | + [Row2Col1, row2_col1_second, Row2Col3], |
| 144 | + [Row3Col1, row3_col1_second, Row3Col3]]) |
| 145 | + |
| 146 | + |
| 147 | + def setDelta3(): |
| 148 | + delta3_array = np.array([[Row1Col1, Row1Col2, row1_col1_second], |
| 149 | + [Row2Col1, Row2Col2, row2_col1_second], |
| 150 | + [Row3Col1, Row3Col2, row3_col1_second]]) |
| 151 | + |
| 152 | + return delta3_array |
| 153 | + # calculating the determinant of matrixes and returns |
| 154 | + |
| 155 | + def get_origin_determinant(): |
| 156 | + return detOriginal |
| 157 | + |
| 158 | + def get_delta1_determinant(ArraySelf): |
| 159 | + tempArray = ArraySelf.det_delta_1 |
| 160 | + return tempArray |
| 161 | + |
| 162 | + def get_delta2_determinant(): |
| 163 | + return det_delta_2 |
| 164 | + |
| 165 | + def get_delta3_determinant(): |
| 166 | + return det_delta_3 |
| 167 | + |
| 168 | + #get final results via three functions |
| 169 | + |
| 170 | + def first_unknown(): |
| 171 | + return det_delta_1/detOriginal |
| 172 | + |
| 173 | + def second_unknown(): |
| 174 | + return det_delta_2/detOriginal |
| 175 | + |
| 176 | + def third_unknown(): |
| 177 | + return det_delta_3/detOriginal |
| 178 | + |
| 179 | + def get_first_array(): |
| 180 | + print('Here we enter the 3 x 3 known matrix') |
| 181 | + |
| 182 | + loopControl = 0 |
| 183 | + while loopControl < 9: |
| 184 | + |
| 185 | + print ('\nEnter Row 1 Column 1') |
| 186 | + Row1Col1 = trial_and_error(input) |
| 187 | + loopControl += 1 |
| 188 | + |
| 189 | + if(loopControl == 1): |
| 190 | + print ('Enter Row 1 Column 2') |
| 191 | + Row1Col2 = trial_and_error(input) |
| 192 | + loopControl += 1 |
| 193 | + |
| 194 | + if(loopControl == 2): |
| 195 | + print ('Enter Row 1 Column 3') |
| 196 | + Row1Col2 = trial_and_error(input) |
| 197 | + loopControl += 1 |
| 198 | + |
| 199 | + if(loopControl == 3): |
| 200 | + print ('\nEnter Row 2 Column 1') |
| 201 | + Row2Col1 =trial_and_error(input) |
| 202 | + loopControl += 1 |
| 203 | + |
| 204 | + if(loopControl == 4): |
| 205 | + print ('Enter Row 2 Column 2') |
| 206 | + Row2Col2 =trial_and_error(input) |
| 207 | + loopControl += 1 |
| 208 | + |
| 209 | + if(loopControl == 5): |
| 210 | + print ('Enter Row 2 Column 3') |
| 211 | + Row2Col3 = trial_and_error(input) |
| 212 | + loopControl += 1 |
| 213 | + |
| 214 | + if(loopControl == 6): |
| 215 | + print ('\nEnter Row 3 Column 1') |
| 216 | + Row3Col1 = trial_and_error(input) |
| 217 | + loopControl += 1 |
| 218 | + |
| 219 | + if(loopControl == 7): |
| 220 | + print ('Enter Row 3 Column 2') |
| 221 | + Row3Col2 = trial_and_error(input) |
| 222 | + loopControl += 1 |
| 223 | + |
| 224 | + if(loopControl == 8): |
| 225 | + print ('Enter Row 3 Column 3') |
| 226 | + Row3Col3 = trial_and_error(input) |
| 227 | + loopControl += 1 |
| 228 | + |
| 229 | + def get_second_array(): |
| 230 | + |
| 231 | + loopControl = 0 |
| 232 | + print ('Here we enter the 3 * 1 known matrix') |
| 233 | + while loopControl < 3: |
| 234 | + |
| 235 | + print ('Enter Row 1 Column 1') |
| 236 | + row1_col1_second = trial_and_error(input) |
| 237 | + loopControl += 1 |
| 238 | + |
| 239 | + if(loopControl == 1): |
| 240 | + print ('Enter Row 2 Column 1') |
| 241 | + row2_col1_second = trial_and_error(input) |
| 242 | + |
| 243 | + loopControl += 1 |
| 244 | + |
| 245 | + if(loopControl == 2): |
| 246 | + print ('Enter Row 3 Column 1') |
| 247 | + row3_col1_second = trial_and_error(input) |
| 248 | + loopControl += 1 |
| 249 | + |
| 250 | + |
| 251 | +#main code, this begins the class constructor and sets all values |
| 252 | +#the final 3 values are for the known solutions for cramers |
| 253 | +# the first 9 digits are to fill a 3 x 3 matrix |
| 254 | +user_array = array_from_user(20, 30, 15, 17, 45, 66, 17, 28, 59, 21, 22, 23) |
| 255 | + |
| 256 | +#array_from_user.get_first_array() |
| 257 | +#array_from_user.get_second_array() |
0 commit comments