1
#This is my code
import numpy as np
import sympy as sp
n=3
y = [sp.symbols('y%d' % i) for i in        range(8*n-4)]
#x=sp.symbols('x:n')
x=np.zeros((1,n))
for i in range(n):
    x[i]=sp.gamma(y[i])
print(x)

I need x[i] to continue the code, but unfortunately, this error occurred. ###################### My modified code is here and the error " '<' not supported between instances of 'tuple' and 'int' " occurred after executing code.

import numpy as np
import sympy as sp
from sympy.matrices import  zeros
from gekko import GEKKO    
# create variables
n=3;z=1

m = GEKKO(remote=False)
x = [sp.symbols('x%d' % i) for i in range(8*n-4)]
#x=sp.symbols('x:20')
t=sp.symbols('t ')

B=np.zeros((n,n))
number = range(n)
for i in number:
   for j in number:
       if i<j :
           B[i][j]=0
       else :
           B[i][j]=sp.factorial(i+j)/(2**j*sp.factorial(j)*sp.factorial(i-j))
PS=[x[0:n]];PI=[x[n:2*n]];PH=[x[2*n:3*n]];PL=[x[3*n:4*n]]
TS=[[1]];TI=[[1]];TH=[[1]];TL=[[1]]
for i in range(n-1):
    TS.append([t**(x[4*n+i]+i+1)])   
    TI.append([t**(x[5*n+i-1]+i+1)])
    TH.append([t**(x[6*n+i-2]+i+1)])
    TL.append([t**(x[7*n+i-3]+i+1)])
S=np.dot(np.dot(PS,B),TS)
I=np.dot(np.dot(PI,B),TI)
H=np.dot(np.dot(PH,B),TH)
L=np.dot(np.dot(PL,B),TL)
DS0=zeros((n,n))
#DI0=np.zeros((n,n));DH0=np.zeros((n,n));DL0=np.zeros((n,n))
for i in number:
    if i==0:
       DS0[i][i]=0
       #DI0[i][i]=0
       #DH0[i][i]=0
      # DL0[i][i]=0
    else:
     DS0[i][i]=sp.gamma(x_4*n+i+1)/sp.gamma(x_4*n+i+1-z)
       
#ss=sp.integrate(s[0][0],(t,0,1))
print (DS0)       
  • 2
    Numpy and sympy are very different libraries, that don't mix well. You created a 2D numpy array of floats. Only floats can be put in there, while you are trying to put sympy expression there. As a general rule, try to not import sympy and numpy (or related libraries such as scipy) into the same part of the code. – JohanC Aug 07 '23 at 10:35
  • Maybe `x = [sp.gamma(y_i) for y_i in y]` fits better with what you are trying to do? – JohanC Aug 07 '23 at 10:39
  • Thanks! you right. according your answer my modified code as bellow. and now the error"'<' not supported between instances of 'tuple' and 'int' " has occurred. – hojat saeidi Aug 07 '23 at 13:24
  • I can not submit new code in a comment. How can i submit it? – hojat saeidi Aug 07 '23 at 13:46
  • 1
    @hojatsaeidi Update your question by [editing](https://stackoverflow.com/posts/76850772/edit) it. More information should always be added to your question rather than as comments. – jared Aug 07 '23 at 13:51

1 Answers1

0

As mentioned in the comments, you're mixing numpy and sympy. If you stick to pure sympy, you can create the y symbols and then use list comprehension in sp.Matrix. To make sure you create a row vector, you can double the parentheses around the entire list comprehension. Though, if you don't actually want a sympy Matrix, you can just use the list comprehension for a regular Python list of gamma function evaluations.

import sympy as sp

n = 3
y = sp.symbols(f"y:{n}")
x = sp.Matrix([[sp.gamma(yi) for yi in y]])
jared
  • 4,165
  • 1
  • 8
  • 31
  • your answer solved first error. my new problem is the error: " '<' not supported between instances of 'tuple' and 'int' " that occurred after executing modified code in my question – hojat saeidi Aug 07 '23 at 14:28
  • That is a completely new question and should be posted as such. You can accept my answer by clicking the check next to it. You can edit your question again to put it back to normal and then open a new question with the updated code. – jared Aug 07 '23 at 14:54