Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends

I need to construct a function which its values come from a matrix's entries.

(From a given matrix)

Please guide me, any ideas are welcome.

The output of the function must be exactly that array.

What I have tried:

Here is a code but I am not sure whether am I doing it right or not

If there is better approach let me know please.

Python
  1  import numpy as np
  2  import math
  3  import matplotlib.pyplot as plt
  4  from scipy import integrate
  5  from scipy.integrate import quad 
  6  import numpy as np
  7  import quadpy
  8  from numpy import linalg as LA
  9  from numpy.linalg import inv
 10  from numpy.linalg import matrix_power
 11  from scipy import linalg
 12  from sympy import *
 13  from numpy.linalg import eig
 14  from scipy.linalg import sqrtm
 15  import scipy.integrate as it
 16  from numpy import vectorize
 17  from scipy.integrate import quad_vec
 18  ############
 19  
 20  from numba import vectorize, float64
 21  ##############################
 22  # Constants
 23  tmax = 20
 24  t = np.arange(0.0, tmax, 1)
 25  t0=0
 26  m=len(t)
 27  #print(t)
 28  
 29  
 30  e=np.matrix([[np.sqrt(0),np.sqrt(1),np.sqrt(2) ,np.sqrt(3),np.sqrt(4),np.sqrt(5),np.sqrt(6),np.sqrt(7),np.sqrt(8),np.sqrt(9)],
 31               [np.sqrt(10),np.sqrt(11),np.sqrt(12), np.sqrt(13),np.sqrt(14),np.sqrt(15),np.sqrt(16),np.sqrt(17),np.sqrt(18),np.sqrt (19)]])
 32  pow = np.power( e, 2 )
 33  pow0=pow[0]
 34  pow1=pow[1]
 35  
 36  def e(xx):
 37        for i in range(0,2):
 38            for xx in np.arange(0.0, tmax, 1):
 39               value= e[i,xx]
 40        return value
 41  y=e(t)
 42  print(y)



As this gives the next error:
Traceback (most recent call last):
  File "C:\Users\user\OneDrive\Desktop\44integral.py", line 41, in <module>
    y=e(t)
  File "C:\Users\user\OneDrive\Desktop\44integral.py", line 39, in e
    value= e[i,xx]
TypeError: 'function' object is not subscriptable
Posted
Updated 15-Aug-21 0:45am
v6
Comments
The Other John Ingram 15-Aug-21 5:48am    
in one instance you are telling python e is a function and then its a 2 dimensional array which is it? e can not be both.
Sara Collins 15-Aug-21 5:59am    
So I need to take a new name instead of e?
Richard MacCutchan 15-Aug-21 9:12am    
You could generate your matrix by using list comprehensions (a powerful feature of Python):
    squareRoots = np.matrix([[np.sqrt(x) for x in range(10)],       # for values 0 - 9
                             [np.sqrt(y) for y in range(10, 20)]])  # for values 10 - 19
Sara Collins 15-Aug-21 14:08pm    
I really appreciate you.

1 solution

As already mentioned you are using e as the name of a variable on line 30, and then as the name of a function on line 36. So how is the interpreter to decide which one you are trying to refer to elsewhere in the code?

You would do well to stop using single letters for the names of variables, and choose names which actually mean something: nouns for objects, and verbs for functions. Apart from anything else it will help people here to try and understand your code.
 
Share this answer
 
Comments
Sara Collins 15-Aug-21 8:31am    
thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900