mmcdara

6214 Reputation

17 Badges

7 years, 316 days

MaplePrimes Activity


These are replies submitted by mmcdara

@Carl Love 

After correction I get these results: horner_stat.mw

Two observations:

  1. The evaluation of the Horner factored polynomial is about twice faster than than the evaluation of the polynomial itself and uses about half the latter's.
    restart
    with(Statistics):
    U := RandomVariable(Uniform(0, 1)):
    P := randpoly(U, dense, degree=500): H := convert(P, horner, U):
    p := unapply(P, U):
    h := unapply(H, U):
    
    SU := Sample(U, 10^4):
    SP := CodeTools:-Usage( p~(SU) ):
    SH := CodeTools:-Usage( h~(SU) ):
    memory used=226.30MiB, alloc change=76.01MiB, cpu time=2.35s, real time=2.35s, gc time=219.37ms
    memory used=152.99MiB, alloc change=32.00MiB, cpu time=1.34s, real time=1.31s, gc time=50.12ms
    So the difference is not that large than the one you display in you answer to @Aliocha
    My question is: Why?
  2. Can you explain me a direct sampling of the two polynomials:
    1. give almost the same performances?
      randomize(170478831571186):
      
      Sh := CodeTools:-Usage( Sample(H, 10^4) ):
      
      randomize(170478831571186):
      
      S  := CodeTools:-Usage( Sample(P, 10^4) ):
      memory used=0.69MiB, alloc change=0 bytes, cpu time=619.00ms, real time=619.00ms, gc time=0ns
      memory used=505.40KiB, alloc change=0 bytes, cpu time=681.00ms, real time=681.00ms, gc time=0ns
      
    2. are much faster than the evaluations of these polynomials on a  U sample (point 1 bove)?

Finally, as the data type emphasize or lessen the differences between Horner and non Horner:

# With Integers

SU := LinearAlgebra:-RandomVector(10^4, generator=-10^3..10^3):
SP := CodeTools:-Usage( p~(SU) ):
SH := CodeTools:-Usage( h~(SU) ):
memory used=4.53GiB, alloc change=64.00MiB, cpu time=8.58s, real time=6.30s, gc time=3.76s
memory used=2.79GiB, alloc change=0 bytes, cpu time=4.30s, real time=2.99s, gc time=2.10s

# With Rationals

S1 := LinearAlgebra:-RandomVector(10^3, generator=1..10^3):
S2 := LinearAlgebra:-RandomVector(10^3, generator=1..10^3):
S3 := 2 *~ LinearAlgebra:-RandomVector(10^3, generator=0..1) -~1:
SU := S3 *~ S1 /~ S2:

SP := CodeTools:-Usage( p~(SU) ):
SH := CodeTools:-Usage( h~(SU) ):
memory used=1.78GiB, alloc change=0 bytes, cpu time=13.81s, real time=13.24s, gc time=925.67ms
memory used=0.95GiB, alloc change=0 bytes, cpu time=1.38s, real time=1.09s, gc time=458.98ms
-

when is Horner factorization useful and when it's useless?

# With Irrationals

S1 := LinearAlgebra:-RandomVector(10^4, generator=1..10^3):
SU := sqrt~(S1):

SP := CodeTools:-Usage( p~(SU) ):
SH := CodeTools:-Usage( h~(SU) ):
memory used=3.88GiB, alloc change=32.00MiB, cpu time=17.81s, real time=11.56s, gc time=8.61s
memory used=1.76GiB, alloc change=32.00MiB, cpu time=12.25s, real time=8.82s, gc time=5.09s

SP[1]; 

3316745734089766397592630110471859650386443147953483621189845470\

  90097763832529787408149010984184954904189345464801382358281506\

  33776102815748690885532435858761523283924372415325847138335927\

  21476776724692220329022997486496244374362690018578817495888580\

  89276047423467309118046638389871685207180192851631794711037526\

  68226650565956387221753112011216542607616820652974153405307803\

  11762352016961376448324782073540088181636152149702927597228541\

  10972110620460287943566861248221758332972512841962698051882950\

  87585311899955236753512717342312862424600797702334851148866019\

  87487905716563591872683982570084096256655551807913554703328334\

                                  (1/2)                          
  2684004425077425823763313576 401      - 4360993989574992282441\

  74655444335506239538092992960307581344624831828308577448248585\

  86643733234979191263532398267659542635477007073264697573690352\

  41184808202886271956377113251918126274819079448214120087602580\

  40976153761457648322734186550766933126965981655745312867389989\

  47273948044111754646777788245934072208799317202380511846729042\

  84664349281405519565375009208274681063255302780948520203565761\

  02283632683447496819722167297580873880176488752482788795280329\

  32859059120794697071801590589198678945797208588083859312946103\

  31831105107339972858373191659906793831837170122360471704338200\

  49578949360502838215949298234767556200734638282783444666508862\

  7426855551

SH[1];
Error, while processing result

@Carl Love 

I've just updated my last reply

@Carl Love 

I think I'm also among those whi have been fooled by these stumbling blocks.
Could you help me understand what makes your results that different than those I uploaded in my initial comment.

And what yours are different from these:
Download horner_stat.mw

@Preben Alsholm 

 I would replace Pi and sqrt(2) by evalf(Pi) and sqrt(2.), respectfully...
Right, I had already noticed that (equation (4) in the file  I attached to my my comment).
I suspected the error was a "display" erro due to the lentgh of the factored expression.

Yhanks for the kernelopts tip.

Interesting question, even if 10 or 12 is two small a degree to hope seeing something.
Nevertheless, using a degree of 500 (in Maple 2015 convert(P(x), horner, x) generates an error if degree(P(x), x)=1000) shows several differences as described in this file horner.mw

In summary, it doesn't seem (that is my own feeling and I will be happy to read the opinion of more skilled members here) that the Horner representation has any real practical interest beyond its educational aspect.

@vv 

because it is the closest of a build-in function (I already found your second solution).
As you're using linalg, I guess there's no LinearAlgebra equivalence, is there?

@Carl Love 

Thanks, this is what I suspected.

@acer 

I didn't know about the `&#`||k||`;` syntax.

Does this mean that  mo(...)mi(...) are only useful if you want to change the font or add modifiers like mathsize and mathcolor for instance (not to mention msup, mover, ... of course)?

@mohandt 

Sorry, I delete my reply

@Axel Vogt 

Thanks Axel.
I had just seen that Combine gave more or less the desired result when I saw your reply.

Unfortunately It doesn't work as it is with Maple 2015 (simplify doesn't drop the numeric part of the integrands out of the integrals... but this can be fixed by doing some extra opertaions).

Thanks again

Updated file: Int_int.mw

@C_R 

Agree, Advanced Search is pitiful

@C_R already answered your question, so just take this as a simple comment:

eq  := a*x^2 + b*x = v;

eq1 := lhs(eq) - rhs(eq);

eq2 := a*x^2 + b*x - v;

a*x^2+b*x = v

 

a*x^2+b*x-v

 

a*x^2+b*x-v

(1)

type~([eq, eq1, eq2], `=`)

[true, false, false]

(2)

map(e -> if type~(e, `=`) then 'lhs'(e) = lhs(e) else "not of type `=`" end if, [eq, eq1, eq2])

[lhs(a*x^2+b*x = v) = a*x^2+b*x, "not of type `=`", "not of type `=`"]

(3)

for e in [eq, eq1, eq2] do
  try
    print('lhs'(e) = lhs(e))
  catch:
  end try
end do:

lhs(a*x^2+b*x = v) = a*x^2+b*x

(4)

select(type, [eq, eq1, eq2], `=`)

[a*x^2+b*x = v]

(5)

selectremove(type, [eq, eq1, eq2], `=`)

[a*x^2+b*x = v], [a*x^2+b*x-v, a*x^2+b*x-v]

(6)

 

Download examples.mw

@sursumCorda 

Another idea which avoid intrducing intermediate points in the definition of the red line rl:

bl   := plot([[1, 0], [6, 5]], color="#0072BD", thickness=3):
lr3D := transform((x, y) -> [x, y, -1/2])(lr):
rr3D := transform((x, y) -> [x, y, 1/2])(rr):

rl := plots:-spacecurve([x, x, tanh(100*(x-7/2))], x=1..6, color=red, thickness=3):

bl3D := transform((x, y) -> [x, y, 0])(bl):

plots:-display(
  lr3D, rr3D, bl3D, 
  rl,
  lightmodel=none, labels=[x, y, z], orientation=[-90, 0, 0]
)


For information: my first try was

plot([[1, 1], [6, 1]], color=red, thickness=3):
plots:-display(
 lr3D, rr3D, bl3D,
 transform((x, y) ->[ x, y, tanh(100*(x-7/2)])(rl)
 ...
)

but for some unknown reason (in Maple 2015) the red line is not "broken and looks like the result of 

plot([[1, 1, -1], [6, 1, 1]], color=red, thickness=3):



For fun this can be generalized this way to make a weawing

For_fun.mw

 

I asked this question a few years ago (I wasn't aware to recover it) and, from memory, I was said the order was predefined my Maple (lines are always before rectangles circles, ...).

Here is a workaround where each line is transformed into a polygon, for instance:

h  := 0.1:
al := [[1, 0], [6, 5]]:
al := [al[], (al[[2, 1]] +~ [[0, h]$2])[]]:
pl := PLOT(POLYGONS(al, COLOR(RGB, ColorTools:-HexToRGB24("#0072BD")[]), STYLE(PATCHNOGRID))):

A_workaround.mw

@Preben Alsholm 

Thanks for these last comments.

(123 is indeed quite large)

To illustrate my previous reply here is a very simple example concerning what I am working on.
The aim here is to prove the relation: variance(V) = mean(V^2) - (mean(V))^2.

Teaching_probability.mw

4 5 6 7 8 9 10 Last Page 6 of 126