A Taylor expansion differentiates without simplifying, so sin(x)/x to 11 terms runs out of memory

Chiusa
#2,291 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Una pull request collegata è già stata integrata.

  • #2302 di @Rafael-SOWNet — integrata

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
performance

Direzione di ricerca

Start with Sources/AngouriMath/Functions/Series.cs, especially SingleTaylorExpansionTerms and MultivariableTaylorExpansionTerms. Reproduce the reported Taylor(sin(x) / x, 11, ("x", 1)) case and compare the derivative growth and runtime. Done means the requested expansion completes efficiently without computing and discarding an extra term; the expected behavior at x = 0 needs triage.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

MathS.Series.Taylor on master (87a0eff), with the heap capped at 4 GB:

Answered
Taylor(sin(x) / x, 7, ("x", 1)) 36 421 characters, 1.4 s
Taylor(sin(x) / x, 9, ("x", 1)) 2 039 500 characters, 87 s
Taylor(sin(x) / x, 11, ("x", 1)) OutOfMemoryException in SingleTaylorExpansion after 89 s
Taylor(sin(x) / x, 11, ("x", 0)) OutOfMemoryException after 42 s; without the cap, on an earlier build, the process passed 35 GB before it was killed
Taylor(sin(x) / x, 4, ("x", 0)) NaN + NaN + NaN / 2! + NaN / 3!

The cause is in SingleTaylorExpansionTerms: each derivative is taken from the previous one as it stands (currExpr = currExpr.Differentiate(exprVariable)), and only the term built from it is simplified. For a quotient the tree grows about eightfold per step:

k nodes of the k-th derivative, as differentiated simplified before the next step
1 11 11
3 122 33
5 2 647 47
7 110 799 69
9 7 797 110 87

Simplifying each derivative before taking the next gives the 11 terms at x = 1 in 0.85 s. Two smaller points in the same place:

  • SingleTaylorExpansion checks terms.Count >= termCount only after the iterator has produced the next term, so one derivative more than is kept (the largest) is taken, substituted and simplified, then dropped. MultivariableTaylorExpansion has the same order.
  • MultivariableTaylorExpansionTerms differentiates the partial derivatives unsimplified in the same way.

What it should have done: answer Taylor(sin(x) / x, 11, ("x", 1)) in about a second. At x = 0, sin(x) / x has no value, so every coefficient is NaN; the expansion should say that once (the step-simplified loop above answers NaN), or expand the continuous extension 1 - x^2/6 + x^4/120 - ..., which is a choice for triage, instead of differentiating ten times to print a sum of NaNs.

Found while building a Taylor-polynomial view for browser demos of the library.

Lingua principale
C#
Stelle
831
Fork
79
Merge medio
1h 12m
PR unite (30g)
518

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ASC-Community/AngouriMath

Tutte le issue di ASC-Community/AngouriMath

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.