Tacit programming: Difference between revisions

Jump to navigation Jump to search
606 bytes removed ,  14:00, 9 January 2020
Line 95: Line 95:


=== Component of a vector in the direction of another vector ===
=== Component of a vector in the direction of another vector ===
Sometimes a train can make an expression nicely resemble its equivalent definition in [[Comparison_with_traditional_mathematics|traditional mathematical notation]]. As an example, here is a program to compute the component of a vector '''a''' in the direction of another vector '''b'''.
Sometimes a train can make an expression nicely resemble its equivalent definition in traditional mathematical notation. As an example, here is a program to compute the component of a vector <math>\textbf{a}</math> in the direction of another vector <math>\textbf{b}</math>:
 
:::<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>
<div style="text-align:center">
<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>
</div>
 
<source lang=apl>
<source lang=apl>
       Sqrt ← *∘0.5            Square root  
       Root ← *∘÷⍨              Nth root
       Norm ← Sqrt+.×⍨         ⍝ Magnitude (norm) of numeric vector in Euclidean space
       Norm ← 2 Root +.×⍨       ⍝ Magnitude (norm) of numeric vector in Euclidean space
       Unit ← ÷∘Norm⍨          ⍝ Unit vector in direction of vector ⍵
       Unit ← ⊢÷Norm            ⍝ Unit vector in direction of vector ⍵
       InDirOf ← (⊢×+.×)∘Unit  ⍝ Component of vector ⍺ in direction of vector ⍵
       InDirOf ← (⊢×+.×)∘Unit  ⍝ Component of vector ⍺ in direction of vector ⍵
       3 5 2 InDirOf 0 0 1      ⍝ Trivial example
       3 5 2 InDirOf 0 0 1      ⍝ Trivial example
0 0 2
0 0 2
</source>
</source>
 
For a more parallel comparison of the notations, see the [[Comparison_with_traditional_mathematics#Practical_example|comparison with traditional mathematics]].  
In particular, the definition of <source inline lang=apl>InDirOf</source> resembles the definition in traditional mathematical notation:
<div style="text-align:center">
{| class="wikitable c" style="margin: 1em auto 1em auto"
! style="width:33%" | Traditional notation !! style="width:33%" | APL
|-
| <math>|\textbf{b}|</math> || <source lang=apl>(Sqrt+.×⍨) b</source>
|-
| <math>\hat{\textbf{b}} = \frac{\textbf{b}}{|\textbf{b}|}</math>  ||  <source lang=apl>(÷∘Norm⍨) b</source>
|-
| <math>\textbf{a}\cdot\textbf{b}</math>  ||  <source lang=apl>a +.× b</source>
|-
| <math>(\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>  ||  <source lang=apl>a (⊢×+.×)∘Unit b</source>
|}
</div>
 
{{APL syntax}}
{{APL syntax}}

Navigation menu