rtoy / maxima

A Clone of Maxima's repo
Other
0 stars 0 forks source link

Sum incorrectly calculated #1435

Closed rtoy closed 2 months ago

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 15:59:50 Created by dzenanz on 2010-08-27 15:30:42 Original: https://sourceforge.net/p/maxima/bugs/2060


The commands below are steps to reproduce. The correct sum for n=5 is 0.25.

(%i2) declare (n, constant); (%o2) done (%i6) sum((0.25+cos(2*i*%pi)+0.5*cos(4*i*%pi))/n,i,0,n-1); (%o6) 1.75

GUI: wxMaxima on Win7 x64 ------------------------------------------------------------- Maxima version: 5.22.1 Maxima build date: 11:48 8/13/2010 Host type: i686-pc-mingw32 Lisp implementation type: GNU Common Lisp (GCL) Lisp implementation version: GCL 2.6.8 -------------------------------------------------------------

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 15:59:52 Created by rtoy on 2010-08-27 17:55:06 Original: https://sourceforge.net/p/maxima/bugs/2060/#5507


Why is the correct answer 0.25?

0.25+cos(2*i*%pi)+0.5*cos(4*i*%pi) = 0.25 + 1 + 0.5 = 1.75 for all integer i.

So it looks like the sum should always be 1.75 to me. Did I miss something?

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 15:59:56 Created by dzenanz on 2010-08-27 19:08:39 Original: https://sourceforge.net/p/maxima/bugs/2060/#0023


You missed division by n (at the end). And I missed division by n within cosines. I will have to check this again on Monday - I guess I have found a bug in my code now :D

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 15:59:59 Created by crategus on 2010-08-29 01:38:47 Original: https://sourceforge.net/p/maxima/bugs/2060/#d09d


I do not see a problem and I get the expected results:

(%i2) sum((1/4+cos(2*i*%pi)+1/2*cos(4*i*%pi))/n,i,0,n-1); (%o2) 7/4

This is the same as

(%i26) 7/4*sum(1/n,i,0,n-1); (%o26) 7/4

When a division by n is missing in the cos functions we have to use the function simplify_sum to get a simplified result:

(%i20) simplify_sum(sum((1/4+cos(2*i*%pi/n)+1/2*cos(4*i*%pi/n))/n,i,0,n-1)); (%o20) 1/4

This again is a correct result. The result does not depend on n.

Setting the status to pending and the resolution to "works for me". Dieter Kaiser

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 16:00:03 Created by crategus on 2010-08-29 01:38:47 Original: https://sourceforge.net/p/maxima/bugs/2060/#3086


rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 16:00:06 Created by dzenanz on 2010-08-30 10:19:28 Original: https://sourceforge.net/p/maxima/bugs/2060/#db12


Sorry, when copying the formula from my C++ code into Maxima for debugging purposes, I missed division by n within cosine functions. This is the correct formula:

ratsimp(sum(0.5*cos((4*%pi*i)/n)+cos((2*%pi*i)/n)+0.25,i,0,n-1)/n);

However, this does not simplify in Maxima. In Derive 6.1, it gives the result I was trying to confirm (the result I suspected by tracing variable values in my code): this sum is equal to 0.25.

So, this can be changed from bug to feature request.

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 16:00:09 Created by dzenanz on 2010-08-30 10:19:29 Original: https://sourceforge.net/p/maxima/bugs/2060/#26b4


rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 16:00:13 Created by rtoy on 2010-08-30 13:49:28 Original: https://sourceforge.net/p/maxima/bugs/2060/#d634


Look at Dieter's comments. By using simplify_sum, he gets the answer of 1/4, as expected.

Marking report as pending.

rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 16:00:17 Created by rtoy on 2010-08-30 13:49:28 Original: https://sourceforge.net/p/maxima/bugs/2060/#dc9f


rtoy commented 2 months ago

Imported from SourceForge on 2024-07-04 16:00:20 Created by dzenanz on 2010-08-30 14:04:06 Original: https://sourceforge.net/p/maxima/bugs/2060/#7d13


This is behavior on my box:

(%i1) declare(n, constant); (%o1) done (%i2) simplify_sum(sum((1/4+cos(2*i*%pi/n)+1/2*cos(4*i*%pi/n))/n,i,0,n-1)); (%o2) simplify_sum(sum(cos((4*%pi*i)/n)/2+cos((2*%pi*i)/n)+1/4,i,0,n-1)/n)

I copied the statement from Dieter's post. I do not get results 1/4 (as Dieter is getting).