4,494
edits
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 | 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> | |||
< | |||
<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math> | |||
<source lang=apl> | <source lang=apl> | ||
Root ← *∘÷⍨ ⍝ Nth root | |||
Norm ← | Norm ← 2 Root +.×⍨ ⍝ Magnitude (norm) of numeric vector in Euclidean space | ||
Unit ← | 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]]. | |||
{{APL syntax}} | {{APL syntax}} |