Carl Love

Carl Love

28085 Reputation

25 Badges

13 years, 99 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

@brian bovril 

(remove~)~(`=`, P, 5);

Each ~ corresponds to one level deeper of nesting.

It's a great Question.

Using a procedure that returns a module as an object constructor has fallen into disfavor. The modern way is to replace your procedure and both modules with a single module that has option object. Here's a minimal working example of your example:

Point:= module()
option object;
local 
   x, y,
   ModuleApply::static:= ((xx, yy)-> Object(Point, _passed)),
   ModuleCopy::static:= proc(mi::Point, proto::Point, xx, yy, $)
      (mi:-x, mi:-y):= `if`(_npassed > 2, [xx,yy], [proto:-x, proto:-y])[]
   end proc,
   ModulePrint::static:= ((mi::Point)-> [mi:-x, mi:-y]),
   ModuleType::static:= (()-> true)
;
export
   `+`::static:= ((p1::Point, p2::Point)-> Point(p1:-x + p2:-x, p1:-y + p2:-y)),
   GetX::static:= ((p::Point)-> p:-x),
   GetY::static:= ((p::Point)-> p:-y),
   Show:= proc(p::Point)
      printf("Point X,Y -> [%f,%f]", p:-x, p:-y);
      NULL
   end proc
;
end module:

Usage:

p1:=Point(1.2,1.4);
p2:=Point(1.0,2.0);

                         p1:= [1.2, 1.4]
                         p2:= [1.0, 2.0]
p2:= p1+p2;
                         p2:= [2.2, 3.4]
Show(p2);
Point X,Y -> [2.200000,3.400000]

Note that this doesn't require the use of option package, option overload, or with; and the use of Show doesn't require a module prefix. Maple uses the fact that the argument to Show is an object to know where to look first for procedure Show. More subtly, Maple uses the fact that the arguments to `+` are objects to know where to look first for the overloaded code for `+`.

Unfortunately, there's a lot of new syntax involved with these objects, as evidenced above. There's a lot of help files to read about this. Start with ?object, ?object,operators, and ?ProgrammingGuide,Chapter09.

Can someone tell me why the vacuous ModuleType in my code above seems to be required even though I don't use any structured types?

We might as well make an approximation of the infinite product, if Maple is willing to do so, rather than truncating at 1001 terms. I have taken the exponential of the logarithm of Mariusz's formula from Wikipedia, because Maple is more willing to evalf infinite Sums than infinite Products.

qgamma:= (z,q)-> evalf(exp((1-z)*ln(1-q) + Sum(ln((1-q^(n+1))/(1-q^(n+z))), n= 0..infinity))):

qgamma(0.8, 0.9);
     1.15699155036365

To convert the bit string to 65, use

convert("01000001", decimal, binary);

To convert it to the character "A", first convert it to 65, then use StringTools:-Char(...).

Also consider using numtheory:-factorset for this purpose. It returns a set of just the prime bases, no exponents. There's nothing different about its underlying algorithm: It simply extracts the bases from the output of ifactors.

Here's another way. It's no better than the other ways presented. I just present it because if you do a lot of work with permutations, you should learn about the conversion to disjoint cycle form, which makes a lot of work with permutations easier. It makes computing the parity of a permutation trivial: It's odd iff there's an odd number of even-length cycles.

parity:= (p::list(posint))-> mul((-1)^~(nops~(convert(p, disjcyc)) -~ 1)):

parity([1, 3, 2, 4, 5]);
     -1

op([1,1,1], ifactor(12));

Remove the restart command from the startup code. The restart command should only be used in the main body of the worksheet.

Here's a new version of fprintf that does what Acer was intending:

My_fprintf:= proc(fp::{nonnegint, name}, form::string:= "%a\n")
   fprintf(fp, form, subsindets([_rest], float, evalf[interface(displayprecision)])[])
end proc:

Example:
Digits:= 25:
interface(displayprecision= 5):
My_fprintf(terminal, evalf(1/3+x/6)):

.33333+.16667*x

Also note that interface(displayprecision) is incorrectly named: It controls the number of digits after the decimal point that are displayed. That's not exactly the same thing as precision. For example:

x:= 0.00000123;
                           x:= 0.00000

That's not five digits precision!

 

Here's a procedure that's significantly faster than Joe's RtableToInverseMap in the case that there are many repeated entries:

InvertRtable:= (A::rtable)-> Threads:-Map(op@((`[]`@lhs)~), ListTools:-Classify(rhs, rtable_elems(A))):

It produces output identical to Joe's except that there are no zeros.

Time comparison:

A:= LinearAlgebra:-RandomMatrix(400$2, generator= rand(0..9)):
CodeTools:-Usage(InvertRtable(A)):

memory used=0.93GiB, alloc change=385.10MiB, cpu time=14.23s, real time=3.11s, gc time=546.88ms
CodeTools:-Usage(RtableToInverseMap(A)):
memory used=9.56GiB, alloc change=128.00KiB, cpu time=38.66s, real time=26.34s, gc time=22.00s

 

I don't think that assuming works with fsolve, but evalc will convert the equations into something that fsolve will handle. In particular, it replaces abs with an equivalent sqrt expression and replaces argument with an equivalent arctan expression.

fsolve(evalc~({eq1, eq2}), {a, z});

     {a = 0.561266211604431e-1, z = .313474043774231}

If x is a decimal number, then the number of places after the decimal point is -op(2,x). This is not necessarily the same as the number of digits of precision of x.

You shouldn't use the same symbol, x, to represent both the unknown in your expression and the initial value of that unknown. I prefer to use x0 to represent the initial value. Here's some corrected code:

simple_iterations:= proc(
   fun::algebraic, x0::complexcons, 
   {epsilon::positive:= 1e-5}, {itermax::posint:= 20000}
)
local N:= indets(fun, And(name, Not(constant))), f, iter, rez, x;
   if nops(N)>1 then error "Too many variables." end if;
   f:= evalf@unapply(fun, [N[]]);
   rez:= x0;
   x:= f(x0);
   for iter to itermax while abs(rez-x) > epsilon do
      rez:= x;
      x:= f(x)
   end do;
   if iter = itermax+1 then error "Did not converge" end if;
   x
end proc:

simple_iterations(1/sin(Pi*x/180), 7);

 

I only have time for a quick Answer right now. The answer to your second question is a definite "no"; it's typical to just use a "large" value to approximate infinity. But the value that you've used is much too large. The values used are typically 5 or 10.

I'll look at your problem in more detail later. One thing that stands out is that none of your IBCs fix the value of x.

Maple's numerical PDE solver is very limited. There must one spatial (space-based) and one temporal (time-based) independent variable, at least implicitly. Which do you consider the spatial, and which do you consider the temporal?

Here is a highly generalizable process for finding the area enclosed by curves. But you must still look at the plot to decide which boundary is which.
 

Problem: Find the area of the region enclosed by y=sqrt(x), x=4 and the x-axis.

 

Solution:

The x-axis is y=0.

Curves:= [y=sqrt(x), x=4, y=0]:  V:= [x,y]:

Use command solve to find intersection points for each pair of curves. The case of one of the curves being a vertical line doesn't need to be treated as a special case.

Pts:= map(solve, combinat:-choose(Curves,2), {V[]});

[{x = 4, y = 0}, {x = 4, y = 2}, {x = 0, y = 0}]

(1)

(minx,maxx,miny,maxy):= (min,max)~((v-> eval~(v, Pts))~(V))[];

0, 4, 0, 2

(2)

xr:= maxx-minx:  yr:= maxy-miny:

plots:-implicitplot(
   Curves, x= minx-xr/3..maxx+xr/3, y= miny-yr/3..maxy+yr/3,
   thickness= 3, gridrefine= 3
);

 

Integrate with respect to x:

(Upper, Lower):= (sqrt(x), 0):

Int(Upper-Lower, x= minx..maxx);

Int(x^(1/2), x = 0 .. 4)

(3)

value(%);

16/3

(4)

There's no need for it here, but just for the sake of generality, I integrate with respect to y also:

solve~(Curves, x);

[y^2, 4]

(5)

(Left,Right):= (y^2, 4):

Int(Right-Left, y= miny..maxy);

Int(-y^2+4, y = 0 .. 2)

(6)

value(%);

16/3

(7)

 


 

Download Area_between.mw

 

First 195 196 197 198 199 200 201 Last Page 197 of 395