Writing Mathematica code in Matlab - matlab

Could anyone please tell me how to do this? I am new to Matlab as well as Mathematica. I have my mathematica coding. But, it gives different results when I run it different time. So, I want to run it in Matlab and verify my result. Please help me anyone.Really appreciate it.
This contains defining functions, parametric plot and etc. I did find thru Matlab. But, I couldn't understand how to write the same program in Matlab.
L1 = 40;
L2 = 20;
A2 = 4.1;
D1 = 1.3;
B1 = 10;
D2 = 19.6;
B2 = 56.6;
N1 = D2 + B2;
N2 = D2 - B2;
A21 = 4.1;
F1 = (D1 \[Pi]^2)/L^2 + (B1 \[Pi]^2)/L^2;
F11 = F1 /. L -> L2;
F2 = (D1 \[Pi]^2)/L^2 - (B1 \[Pi]^2)/L^2;
\[Alpha] = D2^2 - B2^2;
\[Beta] = ((2 \[Pi]^2)/L^2 (D1 D2 - B1 B2) - 2 E1 D2 - A2^2)/\[Alpha];
\[Gamma] = (E1 (E1 - 2 (D1 \[Pi]^2)/L^2) + F1 F2)/\[Alpha];
\[CurlyPhi] = \[Pi]/180*(0);(* input angle in deg*)
\[Kappa]p2 = (-\[Beta] + Sqrt[\[Beta]^2 - 4 \[Gamma]])/2;
\[Kappa]m2 = (-\[Beta] - Sqrt[\[Beta]^2 - 4 \[Gamma]])/2;
\[Kappa]0 = Sqrt[\[Kappa]p2 /. L -> L1];
\[Kappa]01 = Sqrt[-\[Kappa]p2 /. L -> L1];
q = Sqrt[-\[Kappa]m2 /. L -> L1];
q1 = Sqrt[-\[Kappa]p2 /. L -> L2];
q2 = Sqrt[-\[Kappa]m2 /. L -> L2];
(*-----------Electron density \[DoubleStruckCapitalR](\[Rho]) \
:--------------------- *)
R\[Rho] = \[DoubleStruckCapitalN]^2 (p1*Q1*
BesselJ[m, \[Kappa]0*\[Rho]] +
p2*l1*BesselI[m, q*\[Rho]])^2 + \[DoubleStruckCapitalN]^2 (p1*
Q2 BesselJ[m + 1, \[Kappa]0*\[Rho]] +
p2* l2*BesselI[m + 1, q*\[Rho]])^2;
(*\[Rho]<R*)
R\[Rho]1 = \[DoubleStruckCapitalN]^2 (p3*\[CapitalLambda]1*
BesselK[m, q1*\[Rho]] +
p4*\[Beta]1*
BesselK[m,
q2*\[Rho]])^2 + \[DoubleStruckCapitalN]^2 \
(p3*\[CapitalLambda]2*BesselK[m + 1, q1*\[Rho]] +
p4*\[Beta]2*BesselK[m + 1, q2*\[Rho]])^2;(*\[Rho]>R*)
(*---------------Finding p1,p2,p3,p4 -----------------*)
(*
a1 p1+a2 p2+a3 p3==d1;
b1 p1 +b2 p2+b3 p3==d2;
c1 p1+c2 p2+c3 p3==d3;
Subscript[p, 1]=((Subscript[d, 3] Subscript[a, 3]-Subscript[c, 3] \
Subscript[d, 1])(Subscript[b, 2] Subscript[a, 3]-Subscript[b, 3] \
Subscript[a, 2])-(Subscript[d, 2] Subscript[a, 3]-Subscript[b, 3] \
Subscript[d, 1])(Subscript[c, 2] Subscript[a, 3]-Subscript[a, 2] \
Subscript[c, 3]))/((Subscript[c, 1] Subscript[a, 3]-Subscript[c, 3] \
Subscript[a, 1])(Subscript[b, 2] Subscript[a, 3]-Subscript[b, 3] \
Subscript[a, 2])-(Subscript[b, 1] Subscript[a, 3]-Subscript[b, 3] \
Subscript[a, 1])(Subscript[c, 2] Subscript[a, 3]-Subscript[a, 2] \
Subscript[c, 3]));
Subscript[p, 2]=(Subscript[d, 2] Subscript[a, 3]-Subscript[b, 3] \
Subscript[d, 1])/(Subscript[b, 2] Subscript[a, 3]-Subscript[b, 3] \
Subscript[a, 2])-Subscript[p, 1]((Subscript[b, 1] Subscript[a, \
3]-Subscript[b, 3] Subscript[a, 1])/(Subscript[b, 2] Subscript[a, \
3]-Subscript[b, 3] Subscript[a, 2]));
Subscript[p, 3]=Subscript[d, 1]/Subscript[a, 3]-Subscript[a, \
1]/Subscript[a, 3] Subscript[p, 1]-Subscript[a, 2]/Subscript[a, 3] \
Subscript[p, 2
];
Subscript[p, 4]=1;
*)
p1 = -((-b3 c2 d1 + b2 c3 d1 + a3 c2 d2 - a2 c3 d2 - a3 b2 d3 +
a2 b3 d3)/(
a3 b2 c1 - a2 b3 c1 - a3 b1 c2 + a1 b3 c2 + a2 b1 c3 - a1 b2 c3));
p2 = -((b3 c1 d1 - b1 c3 d1 - a3 c1 d2 + a1 c3 d2 + a3 b1 d3 -
a1 b3 d3)/(
a3 b2 c1 - a2 b3 c1 - a3 b1 c2 + a1 b3 c2 + a2 b1 c3 - a1 b2 c3));
p3 = -((-b2 c1 d1 + b1 c2 d1 + a2 c1 d2 - a1 c2 d2 - a2 b1 d3 +
a1 b2 d3)/(
a3 b2 c1 - a2 b3 c1 - a3 b1 c2 + a1 b3 c2 + a2 b1 c3 - a1 b2 c3));
p4 = 1;
a1 = \[Kappa]0 BesselJ[m, \[Kappa]0 R];
a2 = q BesselI[m, q R];
a3 = q1 BesselK[m, q1 R];
b1 = ((F1 /. L -> L1) - E1 + N1 \[Kappa]0^2) BesselJ[
m + 1, \[Kappa]0 R];
b2 = ((F1 /. L -> L1) - E1 - N1 q^2) BesselI[m + 1, q R];
b3 = -(F11 - E1 - N1 q1^2) BesselK[m + 1, q1 R];
c1 = \[Kappa]0^2 BesselJ[m + 1, \[Kappa]0 R];
c2 = -q^2 BesselI[m + 1, q R];
c3 = q1^2 BesselK[m + 1, q1 R];
d1 = -q2 BesselK[m, q2 R];
d2 = (F11 - E1 - N1 q2^2) BesselK[m + 1, q2 R];
d3 = -q2^2 BesselK[m + 1, q2 R];
(*------------Normalization constant---------------*)
\[DoubleStruckCapitalN] =
Sqrt[1/( 2 Pi \
\[DoubleStruckCapitalN]1)];(*1/(\[DoubleStruckCapitalN]^2 2 Pi)=\
\[DoubleStruckCapitalN]1*)
\[DoubleStruckCapitalN]1 = (p1^2*Q1^2*SJJ[m, \[Kappa]0]) + (2*p1*Q1*
p2*l1*SJI[m, \[Kappa]0, q]) + (p2^2*l1^2*SII[m, q]) + (p1^2*Q2^2*
SJJ[m + 1, \[Kappa]0]) + (2*p1*p2*Q2*l2*
SJI[m + 1, \[Kappa]0, q]) + (p2^2*l2^2*
SII[m + 1, q]) + (p3^2*\[CapitalLambda]1^2*SKK[m, q1]) + (2*p3*
p4*\[CapitalLambda]1*\[Beta]1*
SKKab[m, q1, q2]) + (p4^2*\[Beta]1^2*
SKK[m, q2]) + (p3^2*\[CapitalLambda]2^2*SKK[m + 1, q1]) + (2*p3*
p4*\[CapitalLambda]2*\[Beta]2*
SKKab[m + 1, q1, q2]) + (p4^2*\[Beta]2^2*SKK[m + 1, q2]);
Q1 = -A2 \[Kappa]0;
Q2 = (F1 /. L -> L1) - E1 + N1 \[Kappa]0^2;
l1 = -A2 q;
l2 = (F1 /. L -> L1) - E1 - N1 q^2;
\[CapitalLambda]1 = A2 q1;
\[CapitalLambda]2 = F11 - E1 - N1 q1^2;
\[Beta]1 = A2 q2;
\[Beta]2 = F11 - E1 - N1 q2^2;
(*----------Defining the notations----------------*)
SJJ[m_, a_] :=
1/2 R^2 (BesselJ[m, a R]^2 -
BesselJ[-1 + m, a R] BesselJ[m + 1, a R])
SJJab[m_, a_, b_] :=
1/(b^2 - a^2) (a R BesselJ[m, b R ] BesselJ[m - 1, a R] -
b R BesselJ[m - 1, b R] BesselJ[m, a R])
SJI[m_, a_, b_] :=
1/(b^2 + a^2) (-a R BesselI[m, b R ] BesselJ[m - 1, a R] +
b R BesselI[m - 1, b R] BesselJ[m, a R])
SII[m_, a_] :=
1/2 R^2 (BesselI[m, a R]^2 - BesselI[m - 1, a R] BesselI[m + 1, a R])
SIIab[m_, a_, b_] :=
1/(b^2 - a^2) (-a R BesselI[m, b R ] BesselI[m - 1, a R] +
b R BesselI[m - 1, b R] BesselI[m, a R])
SKK[m_, a_] := -(1/2)
R^2 (BesselK[m, a R]^2 - BesselK[m - 1, a R] BesselK[m + 1, a R])
SKKab[m_, a_,
b_] := -(1/(
a^2 - b^2)) (b R BesselK[m, a R ] BesselK[m - 1, b R] -
a R BesselK[m - 1, a R] BesselK[m, b R])
m = 0;
R = 200;
E1 = {0.0888446, 0.153953, 0.24331};
Pm0R200 =
Plot[Piecewise[{{R\[Rho]*10^5, \[Rho] < R}, {R\[Rho]1*10^5, \[Rho] >
R}}], {\[Rho], 0, 250},
AxesLabel -> {Style["\[Rho]", Bold, FontSize -> 18],
Style["|\[CapitalPsi](\[Rho])|\!\(\*SuperscriptBox[\(\\\ \), \
\(2\)]\) (*\!\(\*SuperscriptBox[\(10\), \(-5\)]\))", Bold,
FontSize -> 15]},
BaseStyle -> {FontSize -> 15, FontWeight -> Plain,
FontFamily -> "Times New Roman"}, PlotRange -> Full,
ImageSize -> 700, PlotStyle -> Automatic,
PlotLabel ->
Style["\[DoubleStruckCapitalR](\[Rho]) vs \[Rho] : m=0 & R=200\
\[Angstrom]"]]

Looks like quite a challenge for an inexperienced Matlab user. The Matlab code syntax is different from the Mathematica syntax. You should try to work on something more simple to get to know how Matlab works before trying to write such a difficult script.
Mathworks offers tutorials on their site (I've never done them, but I think it is worth the try) http://www.mathworks.nl/academia/student_center/tutorials/launchpad.html
Also, I found this comparison between the mathematica and matlab syntax, maybe it could help http://amath.colorado.edu/computing/mmm/syntaces.html

Using ToMatlab package you can convert Mathematica expressions to MATLAB equivalents.But it can convert things that have equivalent in MATLAB.
There is a similar question asked in Mathematica.stackexchange.
I don't think conversion of this code to MATLAB is too difficult for a beginner. MATLAB has a very useful ducumentation and you can consult it for your question asked above (about defining functions, Bessel functions, etc in MATLAB)

Related

"fsolve" doesn't work for system nonlinear equation

I have these equations:
syms pm pr teta s
A1 = -2 * b1 * pm + 2 * b2 * pr + b * teta + (1-t) * s + (1-p) * a + c * (b1 - b2);
A2 = 2 * b2 * pm + 2 * b1 * pr + (1-b) * teta + t * s + p * a + c * (b1 - b2);
A3 = b * pm + (1-b) * pr - n * teta - c;
A4 = (1-t) * pm + t * pr - k * s - c;
eqns = [A1,A2,A3,A4];
F=#(pm, pr, teta, s) [A1
A2
A3
A4];
x0 = [10, 10, 10, 10];
fsolve(F, x0)
How I can solve them?
(When I use fsolve, it shows this error: FSOLVE requires all values returned by functions to be of data type double)
Since you tagged Mathematica
A1 = -2*b1*pm + 2*b2*pr + b*teta + (1 - t)*s + (1 - p)*a + c*(b1 - b2);
A2 = 2*b2*pm + 2*b1*pr + (1 - b)*teta + t*s + p*a + c*(b1 - b2);
A3 = b*pm + (1 - b)*pr - n*teta - c;
A4 = (1 - t)*pm + t*pr - k*s - c;
FullSimplify[Solve[{A1 == 10, A2 == 10, A3 == 10, A4 == 10}, {pm, pr, teta, s}]]
pm -> ((k ((-1 + b)^2 + 2 b1 n) + n t^2) (b (10 + c) k +
n (10 + c + 10 k - a k - b1 c k + b2 c k +
a k p - (10 + c) t)) + ((-1 + b) (10 + c) k +
k n (-10 + b1 c - b2 c + a p) - (10 + c) n t) (b k - b^2 k +
n (2 b2 k + t - t^2)))/(k n (1 - 2 b1 k + b^2 (1 + 4 b2 k) +
2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t)))
pr -> (c +
b^2 (10 + c - (-20 + a + 2 b1 c - 2 b2 c) k) + 2 b1^2 c k n -
b2 (20 + c - 2 (-10 + a) k + 2 b2 c k) n - a (1 + 2 b2 k) n p -
2 b1 k (10 + c + n (10 - a p)) + 10 (1 + n - 2 t) -
c (2 + b2 n) t +
n (-30 + a + 20 b2 + a p) t + (10 + c - (-20 + a) n +
2 b2 c n) t^2 - b1 n (c + 20 t + c t (-1 + 2 t)) +
b (k (-10 + a + 20 b1 - 20 b2 - a p) + 20 (-1 + t) +
c (-2 + 3 b1 k - 3 b2 k + 2 t)))/(1 - 2 b1 k +
b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
teta -> (10 - 20 b2 - b2 c -
20 b2 k + 2 a b2 k + 40 b2^2 k + 2 b2^2 c k +
2 b1^2 (20 + 3 c) k - a p -
2 a b2 k p + (-30 + 3 b2 (20 + c) + a (1 + p)) t - (-20 + a +
2 b2 (20 + c)) t^2 +
b (-10 - 4 b1^2 c k + a p +
b1 (20 + 40 k - 2 a k + c (3 + 4 b2 k - 2 t)) + 20 t - a t +
b2 (20 + c - 2 a k + 4 a k p - 2 (20 + c) t)) +
b1 (2 k (-10 + a p) + 20 (-1 + t) + c (-3 + (5 - 2 t) t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
s -> (20 b1 + b1 c - b2 c -
2 b^2 (-10 + b1 c + b2 (20 + c)) + 20 b1 n + 40 b1^2 n -
20 b2 n + 40 b2^2 n + 2 b1^2 c n + 4 b1 b2 c n +
2 b2^2 c n - (b1 - b2) (20 + c) t +
4 b1 (-10 + b1 c - b2 c) n t - 10 (-1 + 2 b2 + t) +
a (-1 - b^2 - 2 b1 n + p + 2 (b1 + b2) n p + t - p t +
2 n (b1 + b2 - 2 b2 p) t - b (-2 + p + t)) +
b (-(-10 + b2 (20 + c)) (-3 + 2 t) + b1 (-20 + c - 2 c t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))

transform matrix and repeat values

I have this matrix:
a1 a2 a3 a4 a5
b1 b2 b3 b4 b5
c1 c2 c3 c4 c5
d1 d2 d3 d4 d5
e1 e2 e3 e4 e5
and i need to transform it to this matrix:
a1 a2 a3 b1 b2 b3 c1 c2 c3
a2 a3 a4 b2 b3 b4 c2 c3 c4
a3 a4 a5 b3 b4 b5 c3 c4 c5
b1 b2 b3 c1 c2 c3 d1 d2 d3
b2 b3 b4 c2 c3 c4 d2 d3 d4
b3 b4 b5 c3 c4 c5 d3 d4 d5
c1 c2 c3 d1 d2 d3 e1 e2 e3
c2 c3 c4 d2 d3 d4 e2 e3 e4
c3 c4 c5 d3 d4 d5 e3 e4 e5
i.e. by taking a 3x3 matrix and iterating over the old matrix putting it as a new row in the new matrix.
How can i do this in matlab? I heard loops are very bad and if I can I should use matrix operations.
If you have access to image processing toolbox you can use im2col which even works for symbolic variables too.
A = sym('A%d%d', [5 5]);
A = sym(A, 'real');
Subs = mat2cell(im2col(A, [3 1])', 3*ones(1,5));
Res = im2col(Subs, [3 1]);
Res is a cell matrix containing the sub matrices.
To concatenate it into a matrix:
reshape([Res{:}]', [9 9])
or if it's not symbolic this also works: cell2mat(Res).
Take a look at this code :
clear
clc
a1 = 1; a2 = 2; a3 = 3; a4 = 4; a5 = 5;
b1 = 11; b2 = 12; b3 = 13; b4 = 14; b5 = 15;
c1 = 21; c2 = 22; c3 = 23; c4 = 24; c5 = 25;
d1 = 31; d2 = 32; d3 = 33; d4 = 34; d5 = 35;
e1 = 41; e2 = 42; e3 = 43; e4 = 44; e5 = 45;
A = [a1 a2 a3 a4 a5; b1 b2 b3 b4 b5; c1 c2 c3 c4 c5; d1 d2 d3 d4 d5; e1 e2 e3 e4 e5];
B = zeros(9,9);
sizeA = size(A);
sizeB = size(B);
jj = 1;
kk = 1;
for ii = 1 : sizeB(1)
B(ii,1:sizeB(1)) = [A(kk, jj:jj+2) A(kk + 1, jj:jj+2) A(kk + 2, jj:jj+2)]
jj = jj + 1;
if mod(ii,3) == 0
kk = kk + 1;
jj = 1;
end
end
I tested it for your case where A is 5x5 and B is 9x9. Might need to be modified to work with different size of A and B.
You can do this in a somewhat general sense, but it is not pretty:
a = [a1, a2, a3, a4, a5];
b = [b1, b2, b3, b4, b5];
% etc.
% Rearrange your vector into a matrix configured how you want it
A = flipdim(toeplitz(a),1); % 5 x 5 matrix in our case
A = A(ceil(size(A,1)/2):size(A,1), ceil(size(A,2)/2):size(A,2)); % now 3 x 3
%%%
% Repeat similar process for b --> B, c --> C, etc.
%%%
% Build the resulting matrix up block-by-block
M = [A, B, C;
B, C, D;
C, D, E];
I was unable to find a good way to generalize the construction of M, and unfortunately forming a toeplitz matrix using toeplitz([a,b,c,d,e]) fails; you do not get the right format for your sub-matrices. Still, you may be able to use this to start on something.

complex looping using Matlab

Let there be five matrices given as:
A= [A1 A1 A1 A1 A1; A2 A2 A2 A2 A2; A3 A3 A3 A3 A3]
B= [B1 B1 B1 B1 B1; B2 B2 B2 B2 B2;B3 B3 B3 B3 B3]
C=[ C1 C1 C1 C1 C1; C2 C2 C2 C2 C2; C3 C3 C3 C3 C3]
D= [D1 D1 D1 D1 D1 ; D2 D2 D2 D2 D2; D3 D3 D3 D3 D3]
E=[ E1 E1 E1 E1 E1; E2 E2 E2 E2 E2; E3 E3 E3 E3 E3]
I want to make a program such that ouput consists of taking each row of each given matrix and forming a new matrix. how to use looping in such cases when length of matrices increases and number of given matrices also increases. This problem seemed to me a complex one. Because I want to generalize by using loop and output for any number of matrices say 20 and having number of columns also increased to say 25, then how to get these P1 to P20 outputs. Can anyone help me regarding this complex trouble using Matlab
P1=[ A1 A1 A1 A1 A1; B1 B1 B1 B1 B1; C1 C1 C1 C1 C1 C1; D1 D1 D1 D1 D1; E1 E1 E1 E1 E1]
P2=[ A2 A2 A2 A2 A2; B2 B2 B2 B2 B2; C2 C2 C2 C2 C2 C2; D2 D2 D2 D2 D2; E2 E2 E2 E2 E2]
and similarly other matrices is obtained .
Note: That the given 5 matrices are generated with help of loop. So first I would be getting values as :
A= A1
B= B1
C=C1
D=D1
E=E1
A= A1 A1
B= B1 B1
C=C1 C1
D=D1 D1
E=E1 E1 .... AND SO ON
Get a loop and put all the matrix together to form a 3D tensor. Or just put the matrices in the 3D tensor when they are created.
M(:,:,1) = A; M(:,:,2) = B; etc
then
squeeze(M(1,:,:))' is the P1, squeeze(M(2,:,:))' is the P2
Example:
M(:,:,1) =
1 2
3 4
M(:,:,2) =
5 6
7 8
>> squeeze(M(1,:,:))'
ans =
1 2
5 6

Solving for the coefficent of linear equations with one known coefficent

clc;
clear all;
syms y a2 a3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [ 0.5 0.25 0.125 ] [ a2 ] [ y ]
% [ 1 1 1 ] [ a3 ] = [ 3 ]
% [ 2 4 8 ] [ 6 ] [ 2 ]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
M = [0.5 0.25 0.125; 1 1 1; 2 4 8];
t = [a2 a3 6];
r = [y 3 2];
sol = M * t'
s1 = solve(sol(1), a2) % solve for a2
s2 = solve(sol(2), a3) % solve for a3
This is what I have so far.
These are my output
sol =
conj(a2)/2 + conj(a3)/4 + 3/4
conj(a2) + conj(a3) + 6
2*conj(a2) + 4*conj(a3) + 48
s1 =
- conj(a3)/2 - 3/2 - Im(a3)*i
s2 =
- conj(a2) - 6 - 2*Im(a2)*i
sol looks like what we would have if we put them back into equation form:
0.5 * a2 + 0.25 * a3 + 0.125 * a4
a2 + a3 + a4 = 3
2*a2 + 4*a3 + 8*a4 = 2
where a4 is known == 6.
My problem is, I am stuck with how to use solve to actually solve these equations to get the values of a2 and a3.
s2 solve for a3 but it doesn't match what we have on paper (not quite).
a2 + a3 + 6 = 3 should yield a3 = -3 - a2.
because of the imaginary. Somehow I need to equate the vector solution sol to the values [y 3 2] for each row.
You need to provide solve function with an equation. Just as simple as that:
sol = solve(M * t' == r');
As a result you have
sol.a2 = 17
sol.a3 = -20
sol.y = 17/4
This works for MATLAB R2012b. In general this can be solved just in one line:
solve('a2 / 2 + a3 / 4 - y + 3 / 4 = 0', 'a2 + a3 + 3 = 0', '2 * a2 + 4 * a3 + 46 = 0')
P.S. I have checked, this works for MATLAB R2011b.

perl - all possible combinations of hash

tell me how to get every possible combination of hash
Here is an example
my %data = (
'a' => [qw(a1 a2 a3)],
'b' => [qw(b1 b2 b3)],
'c' => [qw(c1 c2 c3)]);
to get
a1
a2
a3
b1
b2
b3
c1
c2
c3
a1 b1
a1 b2
a1 b3
a1 c1
a1 c2
a1 c3
b1 c1
b1 c2
b1 c3
b2 c1
b2 c2
b2 c3
b3 c1
b3 c2
b3 c3
a1 b1 c1
a1 b1 c2
a1 b1 c3
a1 b2 c1
a1 b2 c2
a1 b2 c3
a1 b3 c1
a1 b3 c2
a1 b3 c3
a2 b1 c1
a2 b1 c2
a2 b1 c3
a2 b2 c1
a2 b2 c2
a2 b2 c3
a2 b3 c1
a2 b3 c2
a2 b3 c3
a3 b1 c1
a3 b1 c2
a3 b1 c3
a3 b2 c1
a3 b2 c2
a3 b2 c3
a3 b3 c1
a3 b3 c2
a3 b3 c3
thanks
Use brian d foy's Set::CrossProduct module. You'll need to massage your hash into array of arrays in an obvious way.
use Set::CrossProduct;
my $iterator = Set::CrossProduct->new( ARRAY_OF_ARRAYS );
my $tuples = $iterator->combinations;
My module List::Gen contains a cartesian function that can produce the results you want. This code seems to do the trick, but your example does not contain all of the permutations that this will produce, which I am assuming is just an omission in the example.
use List::Gen 'cartesian';
my %data = (
'a' => [qw(a1 a2 a3)],
'b' => [qw(b1 b2 b3)],
'c' => [qw(c1 c2 c3)],
);
my $product = cartesian {join ' ' => sort grep defined, #_}
map {[#$_, undef]}
values %data;
say for sort {length $a <=> length $b or $a cmp $b} #$product;
That is a bit dense, so to explain:
values %data returns the arrays in %data
map {[#$_, undef]} then attaches an empty value to the end of each, since you want the partial combinations
cartesian {join ' ' => sort grep defined, #_} then does the meat of the work, computing the Cartesian product of the arrays while subtracting out the undefined elements, and sorting the values as your example shows.
sort {length $a <=> length $b or $a cmp $b} #$product then prints out the product in the order specified.