Carl Love

Carl Love

26488 Reputation

25 Badges

12 years, 348 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

printf (and lprint) produce only 1d output. Perhaps you can acheive what you want by using the unevaluated form of fi.e., by enclosing it in forward single quotes 'f'.



(**)

f := x-> x^2; a := 0; b := 1;
for i to 5 do  

     print('f'(x[i])=f(a+i*(b-a)/5)*(b-a)/5)

end do;

proc (x) options operator, arrow; x^2 end proc

0

1

f(x[1]) = 1/125

f(x[2]) = 4/125

f(x[3]) = 9/125

f(x[4]) = 16/125

f(x[5]) = 1/5

(**)

 



Download unevaluated.mw

Is that what you want?

 

a(0) = 1

a(n) = 3*2^(n-1), n > 0

Here is the final evalhf/module version of your program. It runs in about 7 seconds; so, about a factor-of-8 improvement over the regular version.

Combi:= module()
export
    probVector::Vector(realcons),  #probability vector
    lambdaVector::Vector(realcons)
;
local
    Combi:= proc(
        k::integer,
        boxContent::Vector,
        activeBox::integer,
        marblesRemaining::integer,
        Output::Matrix,
        Index::Vector,
        p::Vector
    )       
    local j::integer;
        
        if activeBox < k then
            for j from 0 to marblesRemaining do
                boxContent[activeBox]:= j;
                thisproc(k, boxContent, activeBox+1, marblesRemaining-j, Output, Index, p)
            end do
        else
            boxContent[activeBox]:= marblesRemaining;
            Index[1]:= Index[1] + 1;
            Output[1, Index[1]]:= n!*mul(p[j]^boxContent[j], j= 1..k)/mul(boxContent[j]!, j= 1..k);
            Output[2, Index[1]]:= `if`(
                mul(boxContent[j], j= 1..k) = 0,
                undefined,
                -2*add(boxContent[j]*ln(n*p[j]/boxContent[j]), j= 1..k)
            )    
        end if
    end proc,

    ModuleApply:= proc(n::posint, k::posint, p::~Vector(realcons))::identical();
    local
        Index::Vector(realcons), Output::Vector(realcons),
        boxContent::Vector(realcons)  #A vector whose i'th entry holds the number of marbles in the i'th box
    ;
        boxContent:= Vector(k, datatype= float[8]);
        Output:= Matrix(2, binomial(n+k-1, k-1), datatype= float[8]);
        Index:= Vector(1);
        evalhf(Combi(k, boxContent, 1, n, Output, Index, p));
        probVector:= Output[1, ..];
        lambdaVector:= Output[2, ..];
        [][]
    end proc
;
end module;


A recursive procedure

restart

with(Statistics):

n := 40:

k := 6:

p := Vector(k, proc (i) options operator, arrow; 1/k end proc):

(**)

 

Combi:= module()

module () local Combi, ModuleApply; export probVector::(Vector(realcons)), lambdaVector::(Vector(realcons)); end module

``

B := CodeTools[Usage](Combi(n, k, p));

``

Combi:-probVector[1221759];

HFloat(7.480833428389761e-32)

Combi:-lambdaVector[1221759];

HFloat(HFloat(undefined))

NULL


Download A_recursive_procedu.mw


(**)

restart:

(**)

d:= proc(s)  option remember;  parse(s)  end proc:
d("0"):= 1:

(**)

p:= n-> `*`(map(d, StringTools:-Explode(sprintf("%d", n)))[]):

(**)

add(p(n), n= 1..2014);

194785

(**)

 


Download product_digits.mw


1.

(**)

restart:

(**)

B:= 2*A:  C:= 4*A:

(**)

solve(A+B+C = Pi, A);

(1/7)*Pi

(**)

A:= %:

By law of sines:

(**)

b:= sin(B)*a/sin(A):  c:= sin(C)*a/sin(A):

(**)

simplify(1/b + 1/c);

1/a

2.

(**)

simplify(cos(A)^2 + cos(B)^2 + cos(C)^2);

5/4

(**)

 


Download Geometry.mw


(**)

x:= 2014!/5^20:

(**)

d:= 0:

(**)

while d=0 do x:= iquo(x,10,'d') end do:

(**)

d;

6

(**)

 


Download 2014.mw


(**)

restart:

(**)

solve(t^2-2016*t-1, t);

1008+1016065^(1/2), 1008-1016065^(1/2)

(**)

lambda:= %[1]:

(**)

x:= proc(n::posint)
option remember;
     floor(lambda*thisproc(n-1))
end proc;
x(0):= 1;

proc (n::posint) option remember; floor(lambda*thisproc(n-1)) end proc

1

(**)

x(2016) mod 2016;

1009

(**)

 


Download 2016.mw


x:= 4/mul(5^(1/2^k)+1, k= 1..4);

4/((5^(1/2)+1)*(5^(1/4)+1)*(5^(1/8)+1)*(5^(1/16)+1))

(**)

rationalize(%);

`[Length of output exceeds limit of 100000]`

(**)

expand(%);

-1+5^(1/16)

(**)

(%+1)^48;

125

(**)

 


Download 125.mw

Here is an example of multivariate Newton's method. I didn't have access to your equations, so I made some random similar examples.


F:= ['randpoly([y[k] $ k= 1..5], degree= 3) +
      randpoly([y[k] $ k= 1..5], degree= 1, dense)'
    $ 5];

[57*y[1]^2*y[3]-49*y[1]*y[2]*y[5]+31*y[2]^3+73*y[2]*y[5]^2-84*y[1]*y[3]+95*y[1]+148*y[2]-29*y[3]+5*y[4]-26*y[5]-51, 80*y[1]*y[2]*y[5]+90*y[1]*y[3]*y[5]-57*y[1]*y[3]+85*y[4]^2+74*y[1]+27*y[2]+9*y[3]-127*y[4]+81*y[5]+70, 36*y[1]^2*y[3]-8*y[2]^3+30*y[2]*y[3]^2-3*y[3]^2*y[5]-56*y[3]*y[4]^2-5*y[2]^2-91*y[1]-70*y[2]+42*y[3]+9*y[4]-21*y[5]-27, 49*y[1]*y[2]*y[4]-58*y[3]^2*y[5]+49*y[3]*y[5]^2-44*y[2]*y[4]-31*y[2]*y[5]+45*y[5]^2+y[1]-95*y[2]+86*y[3]-97*y[4]-14*y[5]+83, 96*y[1]^2*y[3]-51*y[1]*y[2]*y[3]+89*y[1]*y[3]*y[5]+14*y[2]*y[5]^2-79*y[3]*y[4]^2-58*y[1]-95*y[2]+123*y[3]-2*y[4]+86*y[5]+57]

(**)

fsolve(F);

{y[1] = -1.52634660833751, y[2] = 1.14614000845494, y[3] = -0.912884720559115e-1, y[4] = -.156870015505402, y[5] = 0.268564748179879e-1}

(**)

J:= VectorCalculus:-Jacobian(F, [y[k] $ k= 1..5]):

(**)

Y:= <[1$5]>:  #Dummy first iteration.

(**)

Y1:= <[0$5]>:  #Initial guess

(**)

for n to 100 while LinearAlgebra:-Norm(Y-Y1) > 1e-15 do
     Y:= evalf(Y1);
     Y1:= Y - LinearAlgebra:-LinearSolve(
          eval(J, [seq](y[k]= Y[k], k= 1..5)),
          <eval(F, [seq](y[k]= Y[k], k= 1..5))>
     )
end do:

(**)

n;

7

(**)

Y1;

Vector(5, {(1) = -.937327749126478, (2) = .819756118294004, (3) = -0.193900192863524e-1, (4) = .123223922590096, (5) = -.340268950372420})

Note that we obtained a different solution than fsolve. There are multiple solutions.

(**)

eval(F, [seq](y[k]= Y[k], k= 1..5));

[HFloat(1.4210854715202004e-14), HFloat(0.0), HFloat(0.0), HFloat(0.0), HFloat(7.105427357601002e-15)]

(**)

 


Download MVNewton.mw

You asked about solving this system of equations, or one very similar to it, a few days ago here. I thought that I had convinced you to use fsolve instead of an ad hoc method based on the Jacobian.  You even said "It works like magic." So why aren't you using fsolve?

Addressing the length-of-output issue: The message is not an error message. The output still exists in Maple's memory, and you can still work with it; Maple's GUI just won't print it on the screen.

Assuming that you want to implement a Newton's method, you're going about it all wrong. The first rule of numerical linear algebra: Never invert a matrix; always solve a linear system instead.

Please post your worksheet. I don't get any decimal point. I get this:

inttrans[laplace](diff(diff(f(t),t), t), t, s);

Try doing a restart and doing the command again.

In your dsolve command, include the option output= listprocedure. So, you'll have

reseni:= dsolve(..., output= listprocedure):

Then

Reseni:= eval(dvars, reseni):

Now Reseni is a list of procedures, which can be used as a list-valued procedure, returning a list of pure numbers.

Reseni(tend);

restart:
F:= [sin, cos, tan]:
V:= [0, Pi/6, Pi/4, Pi/3]:
M:= Matrix(nops(F), nops(V), (i,j)-> F[i](V[j]));

M1:= < < `F/V`|``|`<|>`(V) >, `<|>`(``$nops(V)+2), < < F >|< [``$nops(F)] >| M > >;

ssid:= Spread:-CreateSpreadsheet();

Spread:-SetMatrix(ssid, M1);

Your problem is that the with command does not work in procedures. In a procedure, the alternative to with is ?uses . In the procedure's declarations, either immediately before or immediately after the locals, put the statement

uses LinearAlgebra, Student:-MultivariateCalculus;

I see that you have some old, deprecated linalg commands in your procedure. You should replace those with equivalent commands from LinearAlgebra. In particular, there's ?LinearAlgebra,GaussianElimination or ?LinearAlgebra,ReducedRowEchelonForm . Column swapping can now be done with simple indexing:

MM:= M1[.., [5,2..4,1]];

How about

A, B:= Matrix(6, symbol= a), Matrix(6, symbol= b);

First 340 341 342 343 344 345 346 Last Page 342 of 382