## 6346 Reputation

7 years, 351 days

## How to efficiently count the number of r...

Maple

Let M a matrix of zeros and ones for instance.

N := 10^5:  # This is the order of magnitude I have to consider (10^4 may be enough if L is "small")
L := 5:     # L is between 3 and 8 in practice (see context at the end of this post)
M := LinearAlgebra:-RandomMatrix(N, L, generator=0..1):

Let C a set of colums of M and |C| the number of elements of C.
Let P a list of length |C| of  zeros and ones.

I want to count al the rows in M such that

`and`( seq( M[n, C[k]] = P[k], k = 1.. |C| ) )

For instance

pattern := n -> `if`(evalb(`and`(seq(S[n, C[k]]=P[k], k=1..3))), 1, 0):
t0 := time():
time()-t0
12351
0.431  # seconds

This code appears to be quite slow when one consider that I have to run it for all the possible choices of C and P (the contect is detailed below).

Do you have any some ideas to improve the efficiency (in terms of computational time) of this counting operation?

The context:
Let A = {1, 2, .., L} and P(A) the power set of A.
I set Q(A) = P(A) \ ( { } union A}.
Let C and C'  two disjoint subsets of Q(A).
Example

# With L=3

Q(A) = { {1},  {2},  {3},  {1, 2},  {1, 3},  {2, 3} }

# if A chose C =
C = {2}

# Then C' is member of
C' in { {1}, {3}, {1, 3}

For any couple (C, C')  I want to do this

1. Let |C| (|C'|) cardinal of C (resp C').

2. Let us consider that members of C (resp C') "point to" the corresponding columns of M.
for instance C = {2, 3} refers to columns 2 and 3 of M.
As C and C' are ordered iI will write them indistinctely as lists (for instance C = [2, 3] in example above) when necessary.

3. To a given |C| (|C'|) one may associate 2|C| (resp 2|C'|) sequences of 0 and 1.
For instance,  to C = {2, 3} are associated sequences [0, 0], [0, 1], [1, 0], [1, 1].
Let s(C) (s(C')) the list, or set, of 0-1 sequences associated to C (resp C')..

4. For any couple (C, C') and any of the 2|C|+|C'| combinations (p, p') where p belongs to s(C) and p' belongs to s(C') I want to count the number of occurrences defined by:
seq( M[n, C[k]] = p[k], k=1..|C|) and seq( M[n, C'[k]] = p'[k], k=1..|C'|)

For instance, if C={2} and C={1, 3} I want to count the number M rows M such that

p = [0], p'=[1, 0]
M[n, 2]=0 and M[n, 1]=1 and M[n, 3]=0;

p = [1], p'=[1, 1]
M[n, 2]=1 and M[n, 1]=1 and M[n, 3]=1

... and so on

The computational time may ne quite large because of the number of possible couples (C, C') when L is large ( there exists  Stirling2(L+1, 3) such couples: already 90 couples for L=5  ... and 28501 for L=10).

## Why doesn't L2 norm sorting work?...

Maple 2015

Why sorting these 4 vectors wrt L(+oo) norm returns a correct result bur sorting them wrt L2 norm doesn't (unless if I evaluate the norms as floats)?

 > restart:
 > kernelopts(version)
 (1)
 > V := [seq(LinearAlgebra:-RandomVector(2, generator=1..10), k=1..4)]; N2   := evalf(norm~(V, 2)); Ninf := norm~(V, +infinity);
 (2)

Sorting wrt L(+oo) norm

 > sort(V, key=(t -> norm(t, +infinity)));  # correct
 (3)

Sorting wrt L(2) norm

 > sort(V, key=(t -> norm(t, 2))); # not correct is(norm(V[4], 2) < norm(V[3], 2));
 (4)
 > sort(V, key=(t -> evalf(norm(t, 2)))); # correct
 (5)
 >

TIA

## How to color an implicitplot3d?...

Maple 2015

I have a surface defined by C(x, y, x) = 0 that I visualize with implicitplot3d.
Using shading=shue does not suits me and I would like to define my own coloring function F(x, y, z).

The first error I got made me think that a coloring function cannot depend on 3 parameters.
But a simpler (and not visually satysfying function) F(x, y) already leads to an error, which makes me wonder if it is possible to use a colorig function with  implicitplot3d?

implicitplot3d_coloring.mw

## How to convert this trig expression into...

Maple

Question 1
Let theta a real number in the range 0..2*Pi.
Is there a way to "convert"

f := theta -> arccos(cos(theta))+arcsin(sin(theta))

into

f1 := theta -> piecewise(theta < 0, 0, theta < (1/2)*Pi, 2*theta, theta < Pi, Pi, theta <= (3/2)*Pi, 3*Pi-2*theta, 0)

Question 2
As f1 is  2*Pi periodic, is there a way to convert f into a periodic function where the "pattern" is f1(theta)?
To be clearer I thought to something close to what we get when we do this

solve(cos(x)=1, allsolutions);
2 Pi _Z2
Originally _Z1, renamed _Z1~:
is assumed to be: integer

and I imagined

Some_Transformation(f(theta))
f1(theta+2*Pi*_Z1~)
Originally _Z1, renamed _Z1~:
is assumed to be: integer

For the moment I build the periodized f1 this way

f2 := (theta, K) -> int(f1(theta-z)*add(Dirac(z-2*Pi*k), k=-K..K), z=-infinity..+infinity);
# or
f2 := (theta, K) -> sum(int(f1(theta-z)*Dirac(z-2*Pi*k), z=-infinity..+infinity), k=-K..K);

## Why does Statistics:-Mean fails computin...

Maple 2015

For a lesson I'm preparing, I want to illustrate some probability concepts using Maple.
In particular, I need to use the fact that the Expectation operator(let say the Mean operator) is a linear operator with respect to random variables.
However, I don't want to particularize my demonstration by using this or that statistical distribution but simply the notion of random variable.
I have therefore created a specific Distribution named  MinimalAbstractDistribution in which only the mean and variance are defined.

When Statistics:-Mean is applied to the expression (A*p+q) where p and q are names and A is a random variable with distribution MinimalAbstractDistribution, linearity is effectively used.
But not when it is applied to A/p or A-q.

Why that?
Is there a way of defining a statistical distribution so that Mean behaves as expected?

(You will easily understand that no workaround of the form

Mean(A+q);
eval(%, q=-q);

# or

can be accepted in a lesson).

 > restart
 > with(Statistics):

 > MinimalAbstractDistribution := proc(i)   Distribution(Mean=mu[i], Variance=sigma[i]^2) end proc:
 > A := RandomVariable(MinimalAbstractDistribution(1))
 (1)
 > [Mean, Variance](A);
 (2)
 > Mean(p*A+q); Mean(p*(A+q))
 (3)
 > # But Mean(A-q); Mean(A/p)
 (4)