Dear community,
I am new to the forum and I’m looking for a good option for how I can round Julia’s machine accuracy for polynomials or simplify the numbers in the system. I am concerned with the following problems:
I use the package https://github.com/JuliaAlgebra/DynamicPolynomials.jl and would like to save, for example: DynamicPolynomials.Polynomial[1.0000000000000002 + 1.00000000000000004x₂ + 1.1102230246251563e-16x₁ - 2.0000000000000004x₁x₂ - 2.0000000000000004x₁²] as
‘1.0 + 1.0x_2 + 0.0x_1 - 2.0x_1x_2 - 2.0x_1^2.’
Do you have any idea for me how to implement this well?
Thank you very much for your effort!
I’m not saying this is necessarily a ‘good’ solution as there is no clear universal meaning of closeness in this setting (see e.g. discussions about isapprox), but you could use round(..., digits=...) or round(..., sigdigits=...):
As an intermediate step, I would like to simplify this polynomial and then continue my calculations with it. Because this not only increases calculation speed but also simplifies the equations to be calculated.
The simple command round(a) will round a floating-point value a to the nearest integer-valued floating-point number, e.g. round(5.000000000001) == 5.0 but also round(0.75) == 1.0. If that’s desirable, you may also benefit from an explicit conversion to integer data types, e.g. Int(round(a))
The source for that package is a little tough to follow, but it sounds like maybe you’re looking to perform something like
using DynamicPolynominals
poly = Polynomial(...)
rounded_poly = Polynomial(Int.(round.(poly.a)), poly.x)
where the dots before parenthesis are used for broadcasting.
Rounding (without changing types) should not make a difference in this regard, i.e. the computer is equally happy to perform calculations with 5. as it is with 5.1234321. So if this is your main motivation, there is no need to worry about encountering ‘long’ numbers .
If this is for output purposes, the best thing is not to use round (which rounds the internal binary representation), but rather to use @printf (which rounds the decimal-output representation) and related functions from the Printf standard library.
For computational purposes, however, rounding intermediate steps is probably actively a bad idea (not only is it not faster, but it can cause catastrophic loss of accuracy if you are not careful). If for some reason you need to ensure exact integer or rational answers, you need to use exact computations from the beginning (and it will probably be slower).
That’s a good option. Unfortunately, there are not only integers. The goal is to calculate the basis functions for the finite element analysis. There are also 0.25 or something else. For example:
0.25000000001100e10 -> 0.25
round() is probably not an elegant solution, but I will test whether it serves its purpose.