When I assembled a recurrence formula including the following matrix with jupyter notebook, the definition of the recurrence formula itself seems to be working, but I took out one element of the matrix and made a graph to see the change of that element. When I try, I get the following error:
Specifically, the initial state is a matrix called add, and it is a recurrence formula that multiplies it by a matrix called UE and continues to add add.
It appears in a 1x2 matrix, but I want to draw a graph of the element (F (t) ) in the first row of that element at the tth.
add = np.array ([[1,2]]). T UE = np.array ([[1, -1], [-1, 1]]) def F (t): if t == 0: return add else: else: return np.dot (UE, F (t-1)) + add print (F (10)) # [[-1022] # ] print (F (10) ) # [-1022] t = np.arange (0, 1000) plt.plot (t, F (t) )
-------------------------------------------------- ------------------------- ValueError Traceback (most recent call last) <ipython-input-43-c7b4a9e0b37b>in<module> ---->1 plt.plot (t, F (t) ) <ipython-input-40-43c0249f5b44>in F (t) 1 def F (t): ---->2 if t == 0: 3 return add Four 5 else: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any () or a.all ()
Answer # 1
The reason for the error is that the vector value was input to the function F that expects the scalar value to be input, as TakaiY answered.
To achieve what the questioner wants to do, apply numpy.vectorize, which transforms a scalar->scalar function into a vector->vector function. Change the last part as follows.
In addition, since the original function F is a scalar → a vector of two elements, vectorize is applied after making it a scalar → scalar function by shaping it so that only the first element is extracted using the lambda function. I will.
t = np.arange (0, 100) vF0 = np.vectorize (lambda x: F (x) ) plt.plot (t, vF0 (t))
Also, if t is extended to 1000, repeated nesting will exceed the limit of Python, so I set it to 100. I think this improvement goes beyond the premise of the question of using recurrence formulas, so it is not supported.
Answer # 2
The error says that you can't perform comparison operations on multiple arrays.
t = np.arange (0, 1000) plt.plot (t, F (t) )
This is probably because the argument t of the function F requires an integer as a definition, but the array created by np.arrange () is inserted as it is.
- i want to change the value of an element of a multidimensional array in python
- python - how to change the page to redirect when submitting depending on the value selected in the pull-down in the form
- python - does pixel information change when cv2imwrite processing is performed?
- python - count by element with pivot_table in pandas
- i want to delete a partially matching element in a python array
- i want to change the default version of python
- [python] [tkinter] i want to change the acquisition contents of the second and subsequent menu lists by selecting the previous p
- i want to dynamically change the value of a variable in python and execute it
- python 3x - i want to change the background color of the specified line in python3 tkinter treeview
- python - i want to add an element to a double list
- python - i want to retrieve the element above the element searched by soupfind_all in beautiful soup
- python 3x - change font size for python3 tkinter entry
- python - i want to change the ip address that the django app gets by default from remote_addr to http_x_forwarded_for in middlew
- adding an element to a particular line in a python 2d list
- python fourier series the expanded formula is output to the graph
- i want to calculate the recurrence formula in c language
- python - i can't get the element with selenium
- do beginners learn golang? → (change) i want to create a web application with python and automate troublesome things
- python - [ajax] i want to get and send the value of the button element in the form
- I do not know how to deal with ValueError: Length Mismatch at Python.
- How to split 2D array in Python?
- python : Paint the histogram in different colors
- Clustering SCIPY Python
- How can you allocate white glows on ultraviolet image? Python
- Python: how to add a column with a specified value to the beginning of the array
- Gradient from gradient function in Python
- python : Close the Matplotlib interactive window in Jupyter by interrupt button
- python : How to Remove Multiple Rows of NUMPY NDARRAY
- python : Why an error is issued: setting an array element with a sequence