25253

detect if variable is of sympy type

<h3>Question</h3>

I have a variable which may or may not be a sympy class. I want to convert it to a float but I’m having trouble doing this in a general manner:

$ python Python 2.7.3 (default, Dec 18 2014, 19:10:20) [GCC 4.6.3] on linux2 >>> import sympy >>> x = sympy.sqrt(2) >>> type(x) <class 'sympy.core.power.Pow'> >>> y = 1 + x >>> type(y) <class 'sympy.core.add.Add'> >>> z = 3 * x >>> type(z) <class 'sympy.core.mul.Mul'> >>> if isinstance(z, sympy.core): ... z = z.evalf(50) # 50 dp ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

I will be testing x, y, z to convert into a float. note that I can't just run evalf() on x, y and z without checking because they might be integers or floats already and this would raise an exception.

<hr />

sympy.sympify() unfortunately does not convert to float. if it did then that would be the ideal solution to my problem:

>>> sympy.sympify(z) 3*sqrt(2) >>> type(sympy.sympify(z)) <class 'sympy.core.mul.Mul'>
<h3>Answer1:</h3>

All sympy objects inherit from sympy.Basic. To evaluate an sympy expression numerically, just use .n()

In [1]: import sympy In [2]: x = sympy.sqrt(2) In [3]: x Out[3]: sqrt(2) In [4]: x.n() Out[4]: 1.41421356237310 In [5]: if (isinstance(x, sympy.Basic)): ...: print(x.n()) ...: 1.41421356237310
<h3>Answer2:</h3>

heh, at the end of the day, it turns out you can just convert a sympy object to float using python's native float conversion function! don't know why i didn't try this straight away...

>>> import sympy >>> x = sympy.sqrt(2) >>> float(x) 1.4142135623730951

i will use this since it works even if x is a python int or already a python float

来源:https://stackoverflow.com/questions/30023064/detect-if-variable-is-of-sympy-type

Recommend

  • Delete rows based on values within cell, optimising
  • Laravel Eloquent - where relationship field does not equal
  • How to get the cloudinary widget image info on upload?
  • Why NSWindow animator setFrame:display:animate: didn't work sometimes?
  • Square responsive divs using Bootstrap 4
  • How to set timeout on jsf/primefaces ajax request?
  • Using Fetch in react, need username password to access database
  • Go安装以及开发环境搭建
  • Extracting specific cells from multiple Excel files and compile it into one Excel file
  • SyntaxError: invalid label using jQuery.ajax()
  • Python numpy.random.normal
  • curl command a gzipped POST body to an apache server
  • adding seconds to a datetime variable in node.js
  • $routeParams Hard Refresh not working on server in angular js
  • Function which returns multiple values
  • Google fonts in React Native
  • Tabbing behavior in WebForms
  • require() node module from Electron renderer process served over HTTP
  • InfluxDB - Getting only last value in query
  • Using prepared statement in Groovy
  • WebDriver find Element not containing attribute
  • SQL Transform Crosstab Pivot Data
  • Show alternate categorical labels in axis in ggplot2
  • Octopress --> Heroku error env: bundle: no such file or directory
  • Pyinstaller GLIBC_2.15 not found
  • autotest on ubuntu does nothing
  • Runtime complexity of getting the length of a string in different representations
  • Zoho API: Get the user that is making the request
  • how do i compare two rows and store the similarities of the two rows in another column
  • How to use Streams api peek() function and make it work?
  • Will this work on all screen sizes?
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • VSTS work items list through REST API
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • Running R's aov() mixed effects model from Python using rpy2