User:Grondilu: Difference between revisions

From Rosetta Code
Content added Content deleted
(Perl 6 solution)
(change Perl 6 to raku)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{mylangbegin}}
{{mylangbegin}}
{{mylang|Perl 6|<nowiki>****.</nowiki>}}
{{mylang|Raku|<nowiki>****.</nowiki>}}
{{mylang|Perl|<nowiki>**...</nowiki>}}
{{mylang|Perl|<nowiki>**...</nowiki>}}
{{mylang|Bash|<nowiki>***..</nowiki>}}
{{mylang|Bash|<nowiki>***..</nowiki>}}
{{mylang|Octave|<nowiki>*....</nowiki>}}
{{mylang|Octave|<nowiki>*....</nowiki>}}
{{mylang|Wolfram Language|<nowiki>*....</nowiki>}}
{{mylangend}}
{{mylangend}}

=={{header|Perl 6}}==
We're going to solve the example on the Wikipedia article using [https://github.com/grondilu/clifford Clifford], a [https://en.wikipedia.org/wiki/Geometric_algebra geometric algebra] module. Optimization for large vector space does not quite work yet, so it's going to take (a lof of) time and a fair amount of memory, but it should work.

Let's create four vectors containing our input data:

<math>\begin{align}
\mathbf{w} & = w^k\mathbf{e}_k\\
\mathbf{h_0} & = (h^k)^0\mathbf{e}_k\\
\mathbf{h_1} & = (h^k)^1\mathbf{e}_k\\
\mathbf{h_2} & = (h^k)^2\mathbf{e}_k
\end{align}</math>

Then what we're looking for are three scalars <math>\alpha</math>, <math>\beta</math> and <math>\gamma</math> such that:

<math>\alpha\mathbf{h0} + \beta\mathbf{h1} + \gamma\mathbf{h2} = \mathbf{w}</math>

To get for instance <math>\alpha</math> we can first make the <math>\beta</math> and <math>\gamma</math> terms disappear:

<math>\alpha\mathbf{h0}\wedge\mathbf{h1}\wedge\mathbf{h2} = \mathbf{w}\wedge\mathbf{h1}\wedge\mathbf{h2}</math>

Noting <math>I = \mathbf{h0}\wedge\mathbf{h1}\wedge\mathbf{h2}</math>, we then get:

<math>\alpha = (\mathbf{w}\wedge\mathbf{h1}\wedge\mathbf{h2})\cdot\tilde{I}/I\cdot\tilde{I}</math>

<lang perl6>use Clifford;
my @height = <1.47 1.50 1.52 1.55 1.57 1.60 1.63 1.65 1.68 1.70 1.73 1.75 1.78 1.80 1.83>;
my @weight = <52.21 53.12 54.48 55.84 57.20 58.57 59.93 61.29 63.11 64.47 66.28 68.10 69.92 72.19 74.46>;

my $w = [+] @weight Z* @e;

my $h0 = [+] @e[^@height];
my $h1 = [+] @height Z* @e;
my $h2 = [+] (@height X** 2) Z* @e;

my $J = $h1∧$h2;
my $I = $h0∧$J;
say "alpha = ", ($w∧$J)·$I.reversion/($I·$I.reversion).Real;</lang>
{{out}}
<pre>alphas = 128.81280357844</pre>

Latest revision as of 13:15, 10 September 2022

My Favorite Languages
Language Proficiency
Raku ****.
Perl **...
Bash ***..
Octave *....
Wolfram Language *....