72457

How to make sin(pi) and cos(pi/2) zero?

Question:

I understand that in Python sin(pi) and cos(pi/2) won't produce 0, but I'm making calculations with matrices and I need to use those values.

I'm using SymPy and at first the values of sin(pi) and cos(pi/2) are a little annoying. After some multiplications they start to get in the way. Is there a way to make those values be equal to 0 in the entire module? How can I change it in the middle of expressions?

I'll use this matrix as an example:

A = Matrix([ [(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2))*cos(theta3) + (-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(theta3)*cos(alpha3) + sin(alpha2)*sin(alpha3)*sin(theta1)*sin(theta3), -(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2))*sin(theta3) + (-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*cos(alpha3)*cos(theta3) + sin(alpha2)*sin(alpha3)*sin(theta1)*cos(theta3), -(-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(alpha3) + sin(alpha2)*sin(theta1)*cos(alpha3), a3*(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2)) + d2*sin(alpha2)*sin(theta1) - d3*(-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(alpha3) + d3*sin(alpha2)*sin(theta1)*cos(alpha3)], [(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(theta3)*cos(alpha3) + (sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1))*cos(theta3) - sin(alpha2)*sin(alpha3)*sin(theta3)*cos(theta1), (-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*cos(alpha3)*cos(theta3) - (sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1))*sin(theta3) - sin(alpha2)*sin(alpha3)*cos(theta1)*cos(theta3), -(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(alpha3) - sin(alpha2)*cos(alpha3)*cos(theta1), a3*(sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1)) - d2*sin(alpha2)*cos(theta1) - d3*(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(alpha3) - d3*sin(alpha2)*cos(alpha3)*cos(theta1)], [sin(alpha2)*sin(theta2)*cos(theta3) + sin(alpha2)*sin(theta3)*cos(alpha3)*cos(theta2) + sin(alpha3)*sin(theta3)*cos(alpha2), -sin(alpha2)*sin(theta2)*sin(theta3) + sin(alpha2)*cos(alpha3)*cos(theta2)*cos(theta3) + sin(alpha3)*cos(alpha2)*cos(theta3), -sin(alpha2)*sin(alpha3)*cos(theta2) + cos(alpha2)*cos(alpha3),a3*sin(alpha2)*sin(theta2) + d2*cos(alpha2) - d3*sin(alpha2)*sin(alpha3)*cos(theta2) + d3*cos(alpha2)*cos(alpha3)], [0,0,0,1]])

with SymPy I'll substitute the value

substitution = A.subs(alpha2, (-pi/2))

and I'll have a lot of 6.12323399573677e-17 in the middle of it.

Answer1:

Use the symbolic pi from SymPy, not the numeric pi from math or NumPy modules. This is what you are probably doing:

from sympy import sin, cos from math import pi print([sin(pi), cos(pi/2)]) # [1.22464679914735e-16, 6.12323399573677e-17]

And this is what you should do instead:

from sympy import sin, cos, pi print([sin(pi), cos(pi/2)]) # [0, 0]

Answer2:

You could always make a function! Something like

from math import sin as oldsin def sin(x): if x % pi == 0: #if x is an integer mult of pi, like pi, 2pi, -7pi, etc. return 0 else: return oldsin(x)

Answer3:

This is a more general answer than what you may be looking for, but you could try to round down the values to zero. In Numpy, you can do this quite easily: the below example will round any values in the array less than 1e-14 to zero.

threshold = 1e-14 array[array < threshold] = 0

Hope this helps.

Recommend

  • Django admin - How to calculate a field value depending on other model field values
  • Produce a matrix using INDEX and MATCH functions
  • Pandas - Counting quantity of commas in character field
  • Recursively merge dicts so that elements with shared key are combined into a list
  • Creating an instance of a class with the same type as an existing object
  • How to do a Lucene search with Sitecore item with specific date?
  • Replace value in unordered list (html) + JQuery
  • Conflicting Types Error
  • Centering a specific element among others with flexbox [duplicate]
  • Fortran: Array of arbitrary dimension?
  • How to run a query between dates and times?
  • Tips for creating scalable WPF user control
  • Calculate area of cross-section as function of height
  • Is there a way to pivot a customer ID and a their most recent order dates?
  • Google Cloud Platform - Vanity Nameservers
  • Numpy: How to get rid of the minima along axis=1, given the indices - in an efficient way?
  • Merge arrays by common column values in julia
  • Get XML response value with GDataXML
  • 3D Math: Calculate Bank (Roll) angle from Look and Up orthogonal vectors
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • SQL Server re-calculate or not?
  • Not able to display correct data in table -AngularJS
  • Double-click autofill - dynamic based on adjacent cell
  • Use sed with regex and (
  • quiver not drawing arrows just lots of blue, matlab
  • Suppressing passwd when calling sqlplus from shell script
  • Position: fixed nav does not stay fixed
  • Debug.DrawLine not showing in the GameView
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Meteor helpers not available in Angular template
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • vba code to select only visible cells in specific column except heading
  • How do I use the BLAS library provided by MATLAB?
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • python regex in pyparsing
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • using HTMLImports.whenReady not working in chrome