Two months ago,I written a function IRobot
to caculate the $I_{xx},I_{yy},I_{zz}$ and $I_{xy},I_{xz},I_{yz}$,shown as below:
IRobot[IStyle_String, ρ_, VaribleRange_List] := Module[
{IRobotResult},
IRobotResult =
Which[
IStyle == "xx", Integrate[ρ (y^2 + z^2),
{z, VaribleRange[[1, 1]], VaribleRange[[1, 2]]},
{y, VaribleRange[[2, 1]], VaribleRange[[2, 2]]},
{x, VaribleRange[[3, 1]], VaribleRange[[3, 2]]}],
IStyle == "yy", Integrate[ρ (x^2 + z^2),
{z, VaribleRange[[1, 1]], VaribleRange[[1, 2]]},
{y, VaribleRange[[2, 1]], VaribleRange[[2, 2]]},
{x, VaribleRange[[3, 1]], VaribleRange[[3, 2]]}],
IStyle == "zz", Integrate[ρ (x^2 + y^2),
{z, VaribleRange[[1, 1]], VaribleRange[[1, 2]]},
{y, VaribleRange[[2, 1]], VaribleRange[[2, 2]]},
{x, VaribleRange[[3, 1]], VaribleRange[[3, 2]]}],
IStyle == "xy", Integrate[ρ x y,
{z, VaribleRange[[1, 1]], VaribleRange[[1, 2]]},
{y, VaribleRange[[2, 1]], VaribleRange[[2, 2]]},
{x, VaribleRange[[3, 1]], VaribleRange[[3, 2]]}],
IStyle == "xz", Integrate[ρ x z,
{z, VaribleRange[[1, 1]], VaribleRange[[1, 2]]},
{y, VaribleRange[[2, 1]], VaribleRange[[2, 2]]},
{x, VaribleRange[[3, 1]], VaribleRange[[3, 2]]}],
IStyle == "yz", Integrate[ρ y z,
{z, VaribleRange[[1, 1]], VaribleRange[[1, 2]]},
{y,VaribleRange[[2, 1]], VaribleRange[[2, 2]]},
{x,VaribleRange[[3, 1]], VaribleRange[[3, 2]]}]
]
]
And I can use it correctly.
IRobot["xx", ρ, {{-h1, 0}, {-r1, r1}, {-L1 - Sqrt[r1^2 - y^2], Sqrt[r1^2 - y^2]}}]
(*===>*)
1/12 h1 r1 (4 h1^2 (2 L1 + \[Pi] r1) + r1^2 (8 L1 + 3 \[Pi] r1)) ρ
However,I would like to simplify my code by Similarity
,my trail as below:
Flatten /@Thread@List[{z, y, x},
Apply[Part[VaribleRange, ##] &, Table[{i, j}, {i, 1, 3}, {j, 1, 2}], {2}]]
(*==>*)
{{z, VaribleRange[[1, 1]], VaribleRange[[1, 2]]},
{y, VaribleRange[[2, 1]], VaribleRange[[2, 2]]},
{x, VaribleRange[[3, 1]], VaribleRange[[3, 2]]}}
Trial 1
IRobot[IStyle_String, ρ_, VaribleRange_List] := Module[
{IRobotResult,intlist},
intlist=
Flatten /@Thread@List[{z, y, x},
Apply[Part[VaribleRange, ##] &, Table[{i, j}, {i, 1, 3}, {j, 1, 2}], {2}]];
IRobotResult =
Which[
IStyle == "xx", Integrate[ρ (y^2 + z^2),intlist],
IStyle == "yy", Integrate[ρ (x^2 + z^2),intlist],
IStyle == "zz", Integrate[ρ (x^2 + y^2),intlist],
IStyle == "xy", Integrate[ρ x y,intlist],
IStyle == "xz", Integrate[ρ x z,intlist],
IStyle == "yz", Integrate[ρ y z,intlist]
]
]
Unfortunately,it failed.
Trail2
IRobot[IStyle_String, ρ_, VaribleRange_List] := Module[
{IRobotResult,intlist},
intlist[z_,y_,x_,range_]:=
Flatten /@Thread@List[{z, y, x},
Apply[Part[range, ##] &, Table[{i, j}, {i, 1, 3}, {j, 1, 2}], {2}]];
IRobotResult =
Which[
IStyle == "xx", Integrate[ρ (y^2 + z^2),intlist[z,y,x,VaribleRange]],
IStyle == "yy", Integrate[ρ (x^2 + z^2),intlist[z,y,x,VaribleRange]],
IStyle == "zz", Integrate[ρ (x^2 + y^2),intlist[z,y,x,VaribleRange]],
IStyle == "xy", Integrate[ρ x y,intlist[z,y,x,VaribleRange]],
IStyle == "xz", Integrate[ρ x z,intlist[z,y,x,VaribleRange]],
IStyle == "yz", Integrate[ρ y z,intlist[z,y,x,VaribleRange]]
]
]
Also it ends in failure!
Or
I want to by
Which@@(
Flatten@{IStyle == #1, Integrate[ρ #2, intlist[z,y,x,VaribleRange]]} & @@@
{{"xx", (y^2 + z^2)}, {"yy", (x^2 + z^2)}, {"zz", (x^2 + y^2)},
{"xy", x y}, {"xz", x z}, {"yz", y z}})
(*==>*)
to achieve
Which[
IStyle == "xx", Integrate[ρ (y^2 + z^2),intlist[z,y,x,VaribleRange]],
IStyle == "yy", Integrate[ρ (x^2 + z^2),intlist[z,y,x,VaribleRange]],
IStyle == "zz", Integrate[ρ (x^2 + y^2),intlist[z,y,x,VaribleRange]],
IStyle == "xy", Integrate[ρ x y,intlist[z,y,x,VaribleRange]],
IStyle == "xz", Integrate[ρ x z,intlist[z,y,x,VaribleRange]],
IStyle == "yz", Integrate[ρ y z,intlist[z,y,x,VaribleRange]]
]
So my question is why and how to revise it.