|
8 | 8 |
|
9 | 9 | # Free example function definition
|
10 | 10 | # This function passes one of the 11 tests contained inside of test.py. Write the rest, defined in README.md, here, and execute python test.py to test. Passing this precourse work will greatly increase your odds of acceptance into the program.
|
| 11 | +import math |
| 12 | +import numpy as np |
| 13 | + |
| 14 | +def derivative(func , x): |
| 15 | + i = 0.0000000001 |
| 16 | + return round((func(x+i)-func(x))/i,3) |
| 17 | + |
11 | 18 | def f(x):
|
12 | 19 | return x**2
|
13 | 20 |
|
| 21 | +def f_2(x): |
| 22 | + return x**3 |
| 23 | + |
| 24 | +def f_3(x): |
| 25 | + return x**3 + 5*x |
| 26 | + |
| 27 | +def d_f(x): |
| 28 | + return derivative(f,x) |
| 29 | + |
| 30 | +def d_f_2(x): |
| 31 | + return derivative(f_2,x) |
| 32 | + |
| 33 | +def d_f_3(x): |
| 34 | + return derivative(f_3,x) |
| 35 | + |
| 36 | +def vector_sum(vectA ,vectB ): |
| 37 | + vectorAdd = [] |
| 38 | + if(len(vectA) == len(vectB)): |
| 39 | + for x,y in zip(vectA,vectB): |
| 40 | + vectorAdd.append(x+y) |
| 41 | + return vectorAdd |
| 42 | + |
| 43 | +def vector_less(vectA ,vectB ): |
| 44 | + vectorSub = [] |
| 45 | + if(len(vectA) == len(vectB)): |
| 46 | + for x,y in zip(vectA,vectB): |
| 47 | + vectorSub.append(x-y) |
| 48 | + return vectorSub |
| 49 | + |
| 50 | +def vector_magnitude(vectA): |
| 51 | + squaredAddition = 0 |
| 52 | + for x in vectA: |
| 53 | + squaredAddition = squaredAddition + x**2 |
| 54 | + return math.sqrt(squaredAddition) |
| 55 | + |
| 56 | +def vec3(): |
| 57 | + return np.zeros(3) |
| 58 | + |
| 59 | +def vec5(): |
| 60 | + return np.ones(5) |
| 61 | + |
| 62 | +def vec2_1(): |
| 63 | + return np.identity(2)[0] |
| 64 | + |
| 65 | +def vec2_2(): |
| 66 | + return np.identity(2)[1] |
| 67 | + |
| 68 | +def matrix_multiply(matrixA,matrixB): |
| 69 | + try: |
| 70 | + matrixAColNos = np.shape(matrixA)[0] |
| 71 | + except IndexError as e: |
| 72 | + matrixAColNos = 1 |
| 73 | + try: |
| 74 | + matrixBrowNos = np.shape(matrixB)[1] |
| 75 | + except Exception as e: |
| 76 | + matrixBrowNos = 1 |
| 77 | + |
| 78 | + if (matrixAColNos == matrixBrowNos): |
| 79 | + return np.dot(matrixA,matrixB) |
| 80 | + else: |
| 81 | + raise Exception('The dot multiplication is not possible in this case') |
| 82 | + |
0 commit comments