vpa does not work with the desired amount of digits
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to compute a matrix that is the product of some other matrices. What happens is that the entries of those intermediate matrices (not of the final one) have an extremely large number of significant digits that must be correctly expressed, and standard 64-bit precision is not enough at all. However, once that the final matrix has been computed, it does not have any issue and can be safely expressed using 64-bit precision.
Until now, the only thing that seems to work is storing all the intermediate matrices as sym variables, and applying double() to the result. However, this is EXTREMELY slow, so I have tried to use vpa, which runs must faster, but, unfortunately, I am completely unable to make vpa work with the number of digits it should. I have set the variable digits manually, and also introduced the amount of digits as the second parameter of vpa, but Matlab seems completely unable to work with more than thirty some digits.
Explaining the whole problem would be difficult, but a very minimal version might be the following one:
vpa(prod(sym(1:100)), 1000)
returns the correct answer:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000.0
whereas
prod(vpa(1:100,1000))
returns
9.3326215443944152681699238856267e+157
and
vpa(prod(vpa(1:100,1000)),1000)
returns an answer where most digits seem to be random.
93326215443944152681699238856266700490706771753645076823290786995083431421157053646876168787770989280157062278101708191343825014815923981815334275914865311744.0
Any help on this would be greatly appreciated.
0 commentaires
Réponses (1)
Stephen23
le 11 Fév 2025
Modifié(e) : Stephen23
le 11 Fév 2025
You need to set the digits first:
The default is 32.
"returns an answer where most digits seem to be random."
Of course, PROD knows nothing of how many digits you told VPA to use.
vpa(prod(sym(1:100)), 1000)
digits(1000)
prod(vpa(1:100,1000))
vpa(prod(vpa(1:100,1000)),1000)
or simply:
prod(vpa(1:100))
5 commentaires
Stephen23
le 20 Fév 2025
symA = (sym([0,0])+3) .^ 10000
vpaA = (vpa([0,0])+3) .^ 10000
symS = (sym(0)+3) .^ 10000
vpaS = (vpa(0)+3) .^ 10000
"Why does this behavior happen?"
I have no idea. Apparently there is some difference in how VPA encodes scalars vs arrays. I cannot find this documented.
Voir également
Catégories
En savoir plus sur Number Theory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!