- オリジナル住所:線形代数:ベクトル
- 原作:ロディオンChachura
- :からの翻訳ナゲッツへの翻訳プロジェクト
- 固定リンクの記事:github.com/xitu/gold-m ...
- 翻訳:lsvih
- 校正者:エンドン
この記事では、「あるJavaScriptの線形代数チュートリアルの一部」。
ベクターは、正確な表現空間方向のための方法です。値の範囲のベクトルが、各寸法値は、ベクトルであるコンポーネント。次の図では、2次元のベクトル空間の二つの成分の合成を見ることができます。三次元空間において、ベクトルは、3つのコンポーネントで構成されます。
私たちは、2次元のベクトル空間を作成することができますVector2Dのクラスを、その後、3次元でのベクトル空間作成のVector3Dクラスを。しかし、我々は問題を持っている:だけではなく、ベクトルの方向や表現の物理的なスペースのために。例えば、我々は、色(RGBA)が必要になることがあり、それは、4つの構成要素があります、ベクトルとして表される:赤、緑、青、及びアルファチャンネル。また、我々は別の割合を表すためにベクトルを使用して、n個の選択肢(例えば競馬としては、ベクトル確率ゲームに勝つために馬ごとに5を表しています)。したがって、我々はクラス指定されたディメンションを作成し、このようにそれを使用することはありません。
class Vector {
constructor(...components) {
this.components = components
}
}
const direction2d = new Vector(1, 2)
const direction3d = new Vector(1, 2, 3)
const color = new Vector(0.5, 0.4, 0.7, 0.15)
const probabilities = new Vector(0.1, 0.3, 0.15, 0.25, 0.2)
复制代码
ベクトル演算
彼らは次の計算を定義することができ、2つのベクトルの場合を考えてみましょう:
ここで、αはR&LT∈は任意の定数です。
可視化されている事業の外積に加えて、次のことができ、ここで例を見つけます。このGitHubのリポジトリには、プロジェクトおよび関連ライブラリに反応するこれらの視覚的な例を作成するためにも使用します。あなたが反応するようにしてSVGはこれらの例の二次元ビジュアライゼーションを作成する方法を知りたい場合は、を参照してください、この記事。
加減
同様の数値計算は、ベクトル加算と減算をすることができます。ベクトル演算は、ベクトルの各成分があってもよい場合、直接数値計算の結果が得られました:
別のベクトル加算関数をパラメータとして受け取り、対応するベクトル成分を添加し、そして得られた新たなベクトルを返します。同様に、減算機能が、加算減算によって置き換えられます。
class Vector {
constructor(...components) {
this.components = components
}
add({ components }) {
return new Vector(
...components.map((component, index) => this.components[index] + component)
)
}
subtract({ components }) {
return new Vector(
...components.map((component, index) => this.components[index] - component)
)
}
}
const one = new Vector(2, 3)
const other = new Vector(2, 1)
console.log(one.add(other))
// Vector { components: [ 4, 4 ] }
console.log(one.subtract(other))
// Vector { components: [ 0, 2 ] }
复制代码
スケーリング
我々は任意の値とすることができるスケーリング、ベクトルをスケーリングすることができるα-R&LT∈。ズームは、全てのベクトル成分はスケーリング係数が乗算され、α。ときα> 1、ベクトルが長くなる;場合は0≤α<1 、ベクトルが短くなります。場合αは負であり、ベクトルは反対方向に、元のベクトルポインティングをスケーリングします。
でSCALEBYの方法、私たちは、着信のパラメータのベクトル値のすべてのコンポーネントを取り、新しいベクトルを取得するために返却する必要があります。
class Vector {
constructor(...components) {
this.components = components
}
// ...
scaleBy(number) {
return new Vector(
...this.components.map(component => component * number)
)
}
}
const vector = new Vector(1, 2)
console.log(vector.scaleBy(2))
// Vector { components: [ 2, 4 ] }
console.log(vector.scaleBy(0.5))
// Vector { components: [ 0.5, 1 ] }
console.log(vector.scaleBy(-1))
// Vector { components: [ -1, -2 ] }
复制代码
長さ
ピタゴラスの定理により導出ベクトルの長さ:
既存のJavaScript関数は、Mathオブジェクトを内蔵し、その方法は、長さの非常に単純な計算ですので。
class Vector {
constructor(...components) {
this.components = components
}
// ...
length() {
return Math.hypot(...this.components)
}
}
const vector = new Vector(2, 3)
console.log(vector.length())
// 3.6055512754639896
复制代码
ドット積
内積は、2つのベクトルの類似度を計算することができます。2つのベクトルのドット積は、入力方法として、受信し、そして値を出力します。2つのベクトルの内積は、それらの各構成要素との積の対応する和に等しいです。
でドット積の方法、別のベクターは、対応する構成要素と低減方法の積の和で算出、パラメータとして受信しました:
class Vector {
constructor(...components) {
this.components = components
}
// ...
dotProduct({ components }) {
return components.reduce((acc, component, index) => acc + component * this.components[index], 0)
}
}
const one = new Vector(1, 4)
const other = new Vector(2, 2)
console.log(one.dotProduct(other))
// 10
复制代码
我々はいくつかのベクトルの方向との関係を見る前に、私たちは、正規化された方法へのベクターの長さを実装する必要があります。正規化後のこのベクターは、多くのシナリオで使用されます。私たちは宇宙での方向を指定する必要がある場合たとえば、私たちは帰化がこの方向に表現された後のベクターを使用する必要があります。
class Vector {
constructor(...components) {
this.components = components
}
// ...
normalize() {
return this.scaleBy(1 / this.length())
}
}
const vector = new Vector(2, 4)
const normalized = vector.normalize()
console.log(normalized)
// Vector { components: [ 0.4472135954999579, 0.8944271909999159 ] }
console.log(normalized.length())
// 1
复制代码
ドット製品の2つの正規化されたベクトルが1に等しい場合、それは、これら2つのベクトルの方向が同じであることを意味します。我々は、作成areEqual関数は2つの浮動小数点数を比較するために使用されます。
const EPSILON = 0.00000001
const areEqual = (one, other, epsilon = EPSILON) =>
Math.abs(one - other) < epsilon
class Vector {
constructor(...components) {
this.components = components
}
// ...
haveSameDirectionWith(other) {
const dotProduct = this.normalize().dotProduct(other.normalize())
return areEqual(dotProduct, 1)
}
}
const one = new Vector(2, 4)
const other = new Vector(4, 8)
console.log(one.haveSameDirectionWith(other))
// true
复制代码
2つのベクトルのドット積が-1に等しい結果を正規化する場合は、その逆の方向を示しています。
class Vector {
constructor(...components) {
this.components = components
}
// ...
haveOppositeDirectionTo(other) {
const dotProduct = this.normalize().dotProduct(other.normalize())
return areEqual(dotProduct, -1)
}
}
const one = new Vector(2, 4)
const other = new Vector(-4, -8)
console.log(one.haveOppositeDirectionTo(other))
// true
复制代码
ドット製品の2つの正規化ベクトルが0である場合、これは、2つのベクトルが互いに直交していることを意味します。
class Vector {
constructor(...components) {
this.components = components
}
// ...
isPerpendicularTo(other) {
const dotProduct = this.normalize().dotProduct(other.normalize())
return areEqual(dotProduct, 0)
}
}
const one = new Vector(-2, 2)
const other = new Vector(2, 2)
console.log(one.isPerpendicularTo(other))
// true
复制代码
クロス製品
唯一の3次元ベクトルの外積に適用され、それは、2つの入力ベクトルに対して法線ベクトルを生成します。
ときに我々はそれだけで、三次元空間のベクトル演算の範囲内であると仮定すると、クロス積を実現します。
class Vector {
constructor(...components) {
this.components = components
}
// ...
// 只适用于 3 维向量
crossProduct({ components }) {
return new Vector(
this.components[1] * components[2] - this.components[2] * components[1],
this.components[2] * components[0] - this.components[0] * components[2],
this.components[0] * components[1] - this.components[1] * components[0]
)
}
}
const one = new Vector(2, 1, 1)
const other = new Vector(1, 2, 2)
console.log(one.crossProduct(other))
// Vector { components: [ 0, -3, 3 ] }
console.log(other.crossProduct(one))
// Vector { components: [ 0, 3, -3 ] }
复制代码
その他の一般的な方法
実際のアプリケーションでは、上記の方法では十分ではありません。例えば、我々は、2つのベクトル間の角度を見つける1つのベクトルの逆、または別のベクター上に投影されたベクトル、等を計算する必要があるかもしれません。
あなたは方法は、上記述べた記述を開始する前に、我々はラジアン単位の角度との間の変換のために、以下の2つの機能を記述する必要があります。
const toDegrees = radians => (radians * 180) / Math.PI
const toRadians = degrees => (degrees * Math.PI) / 180
复制代码
角度
class Vector {
constructor(...components) {
this.components = components
}
// ...
angleBetween(other) {
return toDegrees(
Math.acos(
this.dotProduct(other) /
(this.length() * other.length())
)
)
}
}
const one = new Vector(0, 4)
const other = new Vector(4, 4)
console.log(one.angleBetween(other))
// 45.00000000000001
复制代码
リバース
あなたは、ベクトル点の方向を逆にする必要がある場合、我々はこのベクトルを-1にスケーリングすることができます。
class Vector {
constructor(...components) {
this.components = components
}
// ...
negate() {
return this.scaleBy(-1)
}
}
const vector = new Vector(2, 2)
console.log(vector.negate())
// Vector { components: [ -2, -2 ] }
复制代码
投影
class Vector {
constructor(...components) {
this.components = components
}
// ...
projectOn(other) {
const normalized = other.normalize()
return normalized.scaleBy(this.dotProduct(normalized))
}
}
const one = new Vector(8, 4)
const other = new Vector(4, 7)
console.log(other.projectOn(one))
// Vector { components: [ 6, 3 ] }
复制代码
設定した長さ
ベクトルの長さを指定するために必要な場合、この方法を使用することができます。
class Vector {
constructor(...components) {
this.components = components
}
// ...
withLength(newLength) {
return this.normalize().scaleBy(newLength)
}
}
const one = new Vector(2, 3)
console.log(one.length())
// 3.6055512754639896
const modified = one.withLength(10)
// 10
console.log(modified.length())
复制代码
平等のために
二つのベクトルが等しいかどうかを決定するために、それらは、対応する構成要素に使用することができるareEqual機能。
class Vector {
constructor(...components) {
this.components = components
}
// ...
equalTo({ components }) {
return components.every((component, index) => areEqual(component, this.components[index]))
}
}
const one = new Vector(1, 2)
const other = new Vector(1, 2)
console.log(one.equalTo(other))
// true
const another = new Vector(2, 1)
console.log(one.equalTo(another))
// false
复制代码
ベースユニットベクトル
我々は、ベクトルとして見ることができる「x軸歩行距離、y軸歩行距離、z軸歩行距離」。我々は使用することができ、そしてより明確に上記の表現値を掛けています。図のケースであり、、軸の単位ベクトル:
任意の数値が乗算されるベクトル、ベクトル成分の値を取得することができる第一の次元に等しいです。例えば:
ベクターは、最も重要な概念はあるあるベース。3次元ベクトルである基底ベクトルのセットである:これは、ベクトルの集合であってもよい座標系の。場合基板のグループ、任意のベクターとすることができる基板の係数に対して表されます。
ベクターは、にして得られる測定の方向に距離測定方向に、距離方向測定得られた距離。
ベースの前に、ベクトルの係数のトリプルのベクトルを知らないと意味がありません。あなただけがに似たベクタベース、知っている数学的なオブジェクトは、現実の世界で(など、色、確率、場所、など)の概念にトリプルを。
あなたは、変換エラーや改善のための他の領域がある見つけた場合は、へようこそデンバー翻訳プログラムは変更と翻訳PRは、また、対応するボーナスポイントを得ることができます。記事の冒頭固定リンクの記事は、 GitHubの上で、この記事内のリンク値下げです。
ナゲッツ翻訳プロジェクトは、技術的な記事のインターネットコミュニティの高品質な翻訳のためのソースでナゲッツに英語を共有する記事。カバーのコンテンツのAndroid、iOSの、フロントエンド、バックエンド、ブロックチェーン、製品、設計、人工知能などの分野は、あなたがより多くの高品質の翻訳を見たいと思って、集中し続ける下さいナゲッツ翻訳プログラムを、公式マイクロブログは、私たちはほとんどの列を知っています。
ます。https://juejin.im/post/5cf61bf8e51d45775653674eで再現