vv

12453 Reputation

19 Badges

9 years, 278 days

MaplePrimes Activity


These are Posts that have been published by vv

add, floats, and Kahan sum

 

I found an intresting fact about the Maple command add for floating point values.
It seems that add in this case uses a summation algorithm in order to reduce the numerical error.
It is probably the Kahan summation algorithm (see wiki), but I wonder why this fact is not documented.

Here is a simple Maple procedure describing and implementing the algorithm.

 

 

restart;

Digits:=15;

15

(1)

KahanSum := proc(f::procedure, ab::range)  
local S,c,y,t, i;      # https://en.wikipedia.org/wiki/Kahan_summation_algorithm
S := 0.0;              # S = result (final sum: add(f(n), n=a..b))
c := 0.0;              # c = compensation for lost low-order bits.
for i from lhs(ab) to rhs(ab) do
    y := f(i) - c;     
    t := S + y;              
    c := (t - S) - y;        
    S := t;                  
od;                         
return S
end proc:

 

Now, a numerical example.

 

 

f:= n ->  evalf(1/(n+1/n^3+1) - 1/(n+1+1/(n+1)^3+1));

proc (n) options operator, arrow; evalf(1/(n+1/n^3+1)-1/(n+2+1/(n+1)^3)) end proc

(2)

n := 50000;
K := KahanSum(f, 1..n);

50000

 

.333313334133301

(3)

A := add(f(k),k=1..n);

.333313334133302

(4)

s:=0.0:  for i to n do s:=s+f(i) od:
's' = s;

s = .333313334133413

(5)

exact:=( 1/3 - 1/(n+1+1/(n+1)^3+1) );

6250249999999900000/18751875067501050009

(6)

evalf( [errK = K-exact, errA = A-exact, err_for=s-exact] );

[errK = 0., errA = 0.1e-14, err_for = 0.112e-12]

(7)

evalf[20]( [errK = K-exact, errA = A-exact, err_for=s-exact] );

[errK = -0.33461e-15, errA = 0.66539e-15, err_for = 0.11166539e-12]

(8)

 


Download KahanSum.mw

There is a bug in inttrans:-hilbert:

restart;

inttrans:-hilbert(sin(a)*sin(t+b), t, s);
# should be:
sin(a)*cos(s+b);   expand(%);

sin(a)*cos(s)

 

sin(a)*cos(s+b)

 

sin(a)*cos(s)*cos(b)-sin(a)*sin(s)*sin(b)

(1)

########## correction ##############

`inttrans/expandc` := proc(expr, t)
local xpr, j, econst, op1, op2;
      xpr := expr;      
      for j in indets(xpr,specfunc(`+`,exp)) do
          econst := select(type,op(j),('freeof')(t));
          if 0 < nops(econst) and econst <> 0 then
              xpr := subs(j = ('exp')(econst)*combine(j/('exp')(econst),exp),xpr)
          end if
      end do;
      for j in indets(xpr,{('cos')(linear(t)), ('sin')(linear(t))}) do
          if type(op(j),`+`) then
              op1:=select(has, op(j),t); ##
              op2:=op(j)-op1;            ##
              #op1 := op(1,op(j));
              #op2 := op(2,op(j));
              if op(0,j) = sin then
                  xpr := subs(j = cos(op2)*sin(op1)+sin(op2)*cos(op1),xpr)
              else
                  xpr := subs(j = cos(op1)*cos(op2)-sin(op1)*sin(op2),xpr)
              end if
          end if
      end do;
      return xpr
end proc:

#######################################

inttrans:-hilbert(sin(a)*sin(t+b), t, s); expand(%);

-(1/2)*cos(a-b)*sin(s)+(1/2)*sin(a-b)*cos(s)+(1/2)*cos(a+b)*sin(s)+(1/2)*sin(a+b)*cos(s)

 

sin(a)*cos(s)*cos(b)-sin(a)*sin(s)*sin(b)

(2)

 


Download hilbert.mw

 

I have just posted an article with this title at Maplesoft Application Center here.
It was motivated by a question posed by  Markiyan Hirnyk  here and a test problem proposed there by Kitonum.

Now I just want to give the promissed complete solution to Kitonum's test:

Compute the plane area of the region defined by the inequalities:

R := [ (x-4)^2+y^2 <= 25, x^2+(y-3)^2 >= 9, (x+sqrt(7))^2+y^2 >= 16 ];

plots:-inequal(R, x=-7..10, y=-6..6, scaling=constrained);

The used procedures (for details see the mentioned article):

ranges:=proc(simpledom::list(relation), X::list(name))
local rez:=NULL, r,z,k,r1,r2;
if nops(simpledom)<>2*nops(X) then error "Domain not simple!" fi;
for k to nops(X) do    r1,r2:=simpledom[2*k-1..2*k][]; z:=X[k];
  if   rhs(r1)=z and lhs(r2)=z then rez:=z=lhs(r1)..rhs(r2),rez; #a<z,z<b
  elif lhs(r1)=z and rhs(r2)=z then rez:=z=lhs(r2)..rhs(r1),rez  #z<b,a<z
  else error "Strange order in a simple domain" fi
od;
rez
end proc:

MultiIntPoly:=proc(f, rels::list(relation(ratpoly)), X::list(name))
local r,rez,sol,irr,wirr, rels1, w;
irr:=[indets(rels,{function,realcons^realcons})[]];
wirr:=[seq(w[i],i=1..nops(irr))];
rels1:=eval(rels, irr=~wirr);
sol:=SolveTools:-SemiAlgebraic(rels1,X,parameters=wirr):
sol:=remove(hastype, eval(sol,wirr=~irr), `=`); 
add(Int(f,ranges(r,X)),r=sol)
end proc:

MeasApp:=proc(rel::{set,list}(relation), Q::list(name='range'(realcons)), N::posint)
local r, n:=0, X, t, frel:=evalf(rel)[];
if indets(rel,name) <> indets(Q,name)  then error "Non matching variables" fi;
r:=[seq(rand(evalf(rhs(t))), t=Q)];
X:=[seq(lhs(t),t=Q)];
to N do
  if evalb(eval(`and`(frel), X=~r())) then n:=n+1 fi;
od;
evalf( n/N*mul((rhs-lhs)(rhs(t)),t=Q) );
end proc:

Problem's solution:

MultiIntPoly(1, R, [x,y]):  # Unfortunately it's slow; patience needed!
radnormal(simplify(value(%)));

evalf(%) = MeasApp(R, [x=-7..10,y=-6..6], 10000); # A rough numerical check
           61.16217534 = 59.91480000

 

# Riemann hypothesis is false! (simple proof)
 

restart;
assume( s>0, s<1/2, t>0 );
coulditbe(abs(Zeta(s+I*t))=0);

                              true

# Q.E.D.

Unfortunately coulditbe(Zeta(s+I*t)=0) returns FAIL, but our assertion is already demonstrated!

The moral: the assume facility deserves a much more careful implementation.


 

A geometric construction for the Summer Holiday

 
Does every plane simple closed curve contain all four vertices of some square?

 This is an old classical conjecture. See:
https://en.wikipedia.org/wiki/Inscribed_square_problem

Maybe someone finds a counterexample (for non-analytic curves) using the next procedure and becomes famous!

 

SQ:=proc(X::procedure, Y::procedure, rng::range(realcons), r:=0.49)
local t1:=lhs(rng), t2:=rhs(rng), a,b,c,d,s;
s:=fsolve({ X(a)+X(c) = X(b)+X(d),
            Y(a)+Y(c) = Y(b)+Y(d),
            (X(a)-X(c))^2+(Y(a)-Y(c))^2 = (X(b)-X(d))^2+(Y(b)-Y(d))^2,
            (X(a)-X(c))*(X(b)-X(d)) + (Y(a)-Y(c))*(Y(b)-Y(d)) = 0},
          {a=t1..t1+r*(t2-t1),b=rng,c=rng,d=t2-r*(t2-t1)..t2});  #lprint(s);
if type(s,set) then s:=rhs~(s)[];[s,s[1]] else WARNING("No solution found"); {} fi;
end:

 

Example

 

X := t->(10-sin(7*t)*exp(-t))*cos(t);
Y := t->(10+sin(6*t))*sin(t);
rng := 0..2*Pi;

proc (t) options operator, arrow; (10-sin(7*t)*exp(-t))*cos(t) end proc

 

proc (t) options operator, arrow; (10+sin(6*t))*sin(t) end proc

 

0 .. 2*Pi

(1)

s:=SQ(X, Y, rng):
plots:-display(
   plot([X,Y,rng], scaling=constrained),
   plot([seq( eval([X(t),Y(t)],t=u),u=s)], color=blue, thickness=2));

 
1 2 3 4 5 6 Page 4 of 6