## 6351 Reputation

7 years, 358 days

## An issue in composing functions...

Maple

Hi,

How can I get y=x in output (4) when  g is equal to f?

 > restart
 > f := x -> F(x)
 (1)
 > ((f@@(-1))@f)(x);
 (2)
 > # Let us assume that y is defined this way y := ((f@@(-1))@g)(x);
 (3)
 > # When g is identical to f I would like to get y=x 'y' = eval(y, g=f); 'y' = eval(y, g = (x -> f(x)))
 (4)
 >

## How can we force evalf[n] to apple to al...

Maple 2015

Here is an example where evalf[n] doesn't operate on the argument of the undefined function f.

```x := rand(0. .. 1.)()
0.2342493224
y := x+f(x):
evalf[4](y)
0.2342 + f(0.2342493224)

# but, as soon as f is a known function:
evalf[4](cos(x))
0.9727

```

Here is a way to force evalf[4](f(x)) to return f(0.2342)?

I found only two ways to do this:
First: declare interface(displayprecision=4)

```interface(displayprecision=4):
y;
0.2342 + f(0.2342)
```

Or: do this (which is relatively cumbersome)

```Evalf := proc(expr, n)
local i := [indets(evalf[n](expr), numeric)[]]:
eval(expr, i =~ evalf[4](i))
end proc:

Evalf(y, 4)
0.2342 + f(0.2342)
```

PS:  I do not like setting displayprecision to some value because its effect is remnant: if you execute again the same worksheet (begining with a restart), the value of displayprecision is not reset to 10 but keeps the value you gave it previously, somewhere in the worksheet.

## Statistics:-Specialize can produce wrong...

Maple 2015

The Statistics package contains a function named Specialize (which quite strangely doesn't appear when you expand the sections of this package).
Here is what help(Specialize) says:

```The Specialize function takes a random variable or distribution data structure that contains symbolic parameters, and performs a substitution to specialize the given random variable or distribution.
```

My goal was to work with mixtures of two random variables. There are many ways to do that depending on the what you really want to achieve, but an elegant way is to define such a mixture this way:

• Let X and Y two random variables representing the two components to be mixed.
For instance X = Normal(mu, sigma) and Y = Normal(nu, tau).

• Let B a Bernoulli random variable with parameter P.

• Then M = B*X + (1-B)*Y represents a random mixture of the two components in proportions (p, 1-p).
Note that M is a 5-parameters random variable.

Doing the things this way enables getting a lot of formal informations about M such as its mean, variance, and so on.

In order to illustrate what the mixture is I draw the histogram of a sample of M.
To do this I Specialized the three random variables X, Y, B.

I used parameters

`mu=-3, nu=3, sigma=1, tau=1, p=1/2`

My first attempt was to draw a sample of the random variable Mspec defined this way

```Mspec := Specialize(B, [p=1/2])*Specialize(X, [mu=-3, sigma=1]) + (1-Specialize(B, [p=1/2]))*Specialize(Y, [nu=3, tau=1]);
```

As you see in the attached file (first plot) the histogram is wrong (so is the variance computed formally).

I changed this into

```Mspec := Specialize(B, [p=1/2])*(Specialize(X, [mu=-3, sigma=1])-Specialize(Y, [nu=3, tau=1])) + Specialize(Y, [nu=3, tau=1]);
```

without more significative success: while the variance is nox corrext the histogram still remains obviously wrong (plot number 2)

My last attempt, which now gives q correct result (plot 3) was:

```Bspec := Specialize(B, [p=1/2]);
Mspec := Bspec*Specialize(X, [mu=-3, sigma=1]) + (1-Bspec)*Specialize(Y, [nu=3, tau=1]);
```

Specialize.mw

I agree that one can easily do this stuff without using  Specialize.
For instance by using the procedure given at the end ofthe attached file.
Or by truly constructing a mixture Distribution (which would be more elegant but more complex).

I also agree that Specialize is in itself of a relative low interest except for educational purposes (you present the theoritical results and next you run a numerical application while giving numeric values to the formal parameters).

But why providing such an anecdotal function if it doesn't do the job correctly?

Note that these results were obtained with Maple 2015, but I doubt they'll be any better for more recent versions, given the confidential nature of Specialize.

## An error in the help(names)...

Maple 2015

In case this error has been corrected in more recent versions, please feel free to delete this question.

In the felp page relative to anames this example is given

```restart:
test1 := 1: test2 := 2: test3 := 3.2: testall := 3.2:
select(type,{anames()},suffixed(`test`));
{test1, test2, test3, testall}
```

Note the backward quotes in suffixed(`test`).
This works only if test is not an assigned name itself:

```restart:
test := 0: test1 := 1: test2 := 2: test3 := 3.2: testall := 3.2:
select(type,{anames()},suffixed(`test`));
Error, (in type/suffixed) expecting a 1st argument of type {string, symbol, list(symbol, string), set(symbol, string)}
```

With simple quotes:

```select(type,{anames()},suffixed('test'));
{test, test1, test2, test3, testall}```

## I'm curious: How does convert/rational ...

Maple

I recently stumbled across this video Why do calculators get this wrong

At some point, the author (Matt Parker) mentions the Farey's algorithm as a way to get a rational approximation of a number between 0 and 1.
Just for fun I applied this algorithm to build increasingly accurate rational approximations of Pi (see here Rational_approximation.mw).
The rational approximation

```convert(evalf(Pi), rational);
104348
------
33215
```

is one of those the Farley's algorithm returns.

Is this pure chance, or does Maple indeed use the Farley's algorithm?
If not, what algorithm does convert/rational use?

 1 2 3 4 5 6 7 Last Page 3 of 43
﻿