[PYTHON] I wanted to calculate an array with Sympy's subs method

It seems that the subs method is useless, and it seems that you need to use a function called sympy.lambdify.

subs_vs_lambdify.py


# coding:utf-8


import numpy as np
import sympy as sp


#Create a sympy variable
x = sp.Symbol('x')
y = sp.Symbol('y')

#Make an array of numpy
arrX = np.arange(12, dtype='float64').reshape((3, 4))
arrY = np.ones((3, 4))

#Create a sympy function
symbolFunc = 2*x + y

#Assign an array with the subs method
resSubs = symbolFunc.subs([(x, arrX), (y, arrY)])
print(resSubs)
# ==> 2*x + y
#It seems that the array cannot be calculated

#Try to calculate using lambdify
#First, create a function
lambdifyFunc = sp.lambdify([x, y], symbolFunc)
#Pass an array as an argument of the created function
resLambdify = lambdifyFunc(arrX, arrY)
print(resLambdify)
# ==> [[ 1.  3.  5.  7.]
#      [ 9. 11. 13. 15.]
#      [17. 19. 21. 23.]]
#This one calculated

Recommended Posts

I wanted to calculate an array with Sympy's subs method
I wanted to solve ABC160 with Python
I wanted to solve ABC172 with Python
I really wanted to copy with selenium
I wanted to solve NOMURA Contest 2020 with Python
Add rows to an empty array with numpy
I wanted to play with the Bezier curve
I wanted to install Python 3.4.3 with Homebrew + pyenv
I want to be an OREMO with setParam!
How to read an array with Python's ConfigParser
I tried to detect an object with M2Det!
I want to convert an image to WebP with lollipop
I also wanted to check type hints with numpy
I tried to implement an artificial perceptron with python
I tried to make an OCR application with PySimpleGUI
I tried to find an alternating series with tensorflow
How to convert an array to a dictionary with Python [Application]
I tried it with SymPyLive by referring to "[Ruby] Getting unique elements from an array".
I made an animation to return Othello stones with POV-Ray
I wanted to delete multiple objects in s3 with boto3
I tried to create an article in Wiki.js with SQLAlchemy
I wanted to do it like running an AtCoder test case.
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
I want to use an external library with IBM Cloud Functions
I wanted to solve the ABC164 A ~ D problem with Python
I tried to make an image similarity function with Python + OpenCV
Hash chain I wanted to avoid (2)
I sent an SMS with Python
I wanted to evolve cGAN to ACGAN
How to calculate date with python
I want to do ○○ with Pandas
I want to debug with Python
Hash chain I wanted to avoid (1)
I wanted to use jupyter notebook with docker in pip environment (opticspy)
I tried to make an open / close sensor (Twitter cooperation) with TWE-Lite-2525A
It's more recent, but I wanted to do BMI calculation with python.
I get an error with import pandas.
I tried sending an SMS with Twilio
I tried to implement Autoencoder with TensorFlow
I tried to visualize AutoEncoder with TensorFlow
I tried to get started with Hy
I tried sending an email with python.
When I get an error with PyInstaller
I want to analyze logs with Python
I want to play with aws with python
[Introduction to Pytorch] I played with sinGAN ♬
I wanted to solve ABC159 in Python
What I did with a Python array
I tried to implement CVAE with PyTorch
I want to make an automation program!
Convert array (struct) to json with golang
Be careful when adding an array to an array
I tried to solve TSP with QAOA
[Python] I tried to calculate TF-IDF steadily
Try to generate an image with aliasing
Implemented DQN in TensorFlow (I wanted to ...)
I want to send Gmail with Python, but I can't because of an error
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
I want to write an element to a file with numpy and check it.
I tried to extract a line art from an image with Deep Learning
I tried to create an environment to check regularly using Selenium with AWS Fargate