-
Notifications
You must be signed in to change notification settings - Fork 63
warmUpExcersise in Ipython
PKHG edited this page Jul 20, 2012
·
4 revisions
Need to know how to show Python code! If I know how this wiki behaves I will post an idea Result: to insert code one has to indent each line with at least 4 spaces or a tab. INFO problems with ... to be solved ..
#Octave function A = warmUpExercise()
#PKHG solution needs: schould be found be a student!!!!! module info??!!
from pylab import eye as eye
#Python warmUpExercise should return a 5 x 5 identity (as ???!!!)
def warmUpExercise():
#WARMUPEXERCISE Example function in octave
#Octave A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix
A = [];
#Octave ============= YOUR CODE HERE ==============
#Octave Instructions: Return the 5x5 identity matrix
#Octave In octave, we return values by defining which variables
#Octave represent the return values (at the top of the file)
#Octave and then set them accordingly.
#Python ============= YOUR CODE HERE ==============
#Python Instructions: Return the 5x5 identity matrix
#Python In Ipython, we return values by defining which variables
#Python represent the return values (at the top of the file)
#Python and then set them accordingly.
A = eye(5,5)
A[1][2] = 0.1
# A[2][1] = 0.1
# =============end of your code==============================
#Octave end
return A
if __name__ == "warmUpExercise":
tmp = warmUpExercise()
if tmp == []:
print("sorry, not good yet", tmp)
else:
from numpy import ndarray
from pylab import shape
from pylab import diag
if type(tmp) != ndarray:
print("sorry, not good yet, the result has to be of type numpy.ndarray,")
print("yours is ", type(tmp))
elif shape(tmp) != (5,5):
print("sorry, not good yet, dimension of Identiy not ok your is")
print("yours is ", shape(tmp))
else:
tmp1 = all(diag(tmp) == 1)
tmp3 = float(sum(sum(abs(tmp))))
tmp2 = tmp3 == 5.0
# print("tests" , tmp1, tmp3,tmp2)
if not (tmp1 and tmp2):
print("not all diagonal elemens 1 or containing non-zeros outside the diagonal, your result")
print(tmp)
else:
print("Correct: Your result is an 5x5 identity ")
print(tmp)
print("")