From d33728c8c9a525934ee6bc396aca8cf5a985f20a Mon Sep 17 00:00:00 2001 From: Ron Bessems Date: Sat, 15 Dec 2018 17:36:45 -0500 Subject: [PATCH] Version 0.2.0 More Vector functionality. * Added Vector.sum, Vector.mean, Vector.norm, Vector.manhattanNorm and Vector.toList with tests and docs. * Deprecated Vector.distance for the better name Vector.norm. --- CHANGELOG.md | 9 +++- lib/vector.dart | 96 ++++++++++++++++++++++++++++++++++++++++++- pubspec.yaml | 2 +- test/vector_test.dart | 16 ++++++++ 4 files changed, 119 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 920b746..6c9e335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,14 @@ -## [0.10.1] -- 11/27/2018 +## [0.2.0] -- 12/15/2018 + +* Added Vector.sum, Vector.mean, Vector.norm, Vector.manhattanNorm and Vector.toList with tests and docs. +* Deprecated Vector.distance for the better name Vector.norm. + +## [0.1.1] -- 11/27/2018 * Documentation updates * Added Matrix.trace, Vector.crossProduct and Vector.dotProduct -## [0.10.0] -- 11/26/2018 +## [0.1.0] -- 11/26/2018 * Slight documentation modifications. diff --git a/lib/vector.dart b/lib/vector.dart index de710ef..156354b 100644 --- a/lib/vector.dart +++ b/lib/vector.dart @@ -53,6 +53,10 @@ class Vector { /// Creates a row vector from the list of values given. /// + /// Note that a reference to the list is used, not a deep + /// copy. If you change the [values] List after passing + /// into this vector the vector changes as well. + /// /// ```dart /// Vector v = Vector.row([1.0,2.0,3.0]); /// print(v); @@ -156,7 +160,21 @@ class Vector { /// /// will print 1.7320508 /// + @deprecated double magnitude() { + return norm(); + } + + /// Returns the [Euclidean norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm) of the vector. + /// + /// ```dart + /// Vector v = Vector([1.0, 1.0, 1.0]); + /// print(v.norm()); + /// ``` + /// + /// will print 1.7320508 + /// + double norm() { double v = 0.0; for (int i = 0; i < elements; i++) { v += this[i] * this[i]; @@ -164,6 +182,57 @@ class Vector { return sqrt(v); } + /// Returns the [Manhattan norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#Taxicab_norm_or_Manhattan_norm) of the vector. + /// + /// ```dart + /// Vector v = Vector([1.0, 1.0, 1.0]); + /// print(v.manhattanNorm()); + /// ``` + /// + /// will print 3.0 + /// + double manhattanNorm() { + double v = 0.0; + for (int i = 0; i < elements; i++) { + v += this[i].abs(); + } + return v; + } + + /// Returns element wise sum of the vector. + /// + /// ```dart + /// Vector v = Vector([1.0, -1.0, 1.0]); + /// print(v.sum()); + /// ``` + /// + /// will print 1.0 + /// + double sum() { + double v = 0.0; + for (int i = 0; i < elements; i++) { + v += this[i]; + } + return v; + } + + /// Returns element wise mean of the vector. + /// + /// ```dart + /// Vector v = Vector([1.0, 1.0, 1.0]); + /// print(v.mean()); + /// ``` + /// + /// will print 1.0 + /// + double mean() { + double v = 0.0; + for (int i = 0; i < elements; i++) { + v += this[i]; + } + return v / elements; + } + /// [Transposes](https://en.wikipedia.org/wiki/Transpose) a Vector from row or column to column or row. /// /// ```dart @@ -292,7 +361,7 @@ class Vector { /// [0.5, 0.5, 0.5, 0.5] /// ``` Vector normalize() { - return Vector.fromMatrix(this._matrix * (1.0 / magnitude())); + return Vector.fromMatrix(this._matrix * (1.0 / norm())); } /// Calculates the [cross product](https://en.wikipedia.org/wiki/Cross_product) for two 3-dimensional vectors. @@ -375,7 +444,32 @@ class Vector { } /// Returns the [Matrix] representation of this [Vector] + /// + /// Note this is a reference to the internal matrix of the + /// vector. Changes to the returned matrix will also affect the + /// vector. Matrix toMatrix() { return _matrix; } + + /// Returns the [List] of this [Vector] + /// + /// Note if the vector is a row vector a reference to the + /// internal data is returned. Any changes to that data + /// affect the vector as well. + /// + /// If the vector is a column vector a copy of the vector + /// is returned and changes to the returned list will + /// _not_ affect the vector. + List toList() { + if (_vectorType == VectorType.row) { + return this._matrix[0]; + } else { + List l = List(this.elements); + for (int i = 0; i < this.elements; i++) { + l[i] = this[i]; + } + return l; + } + } } diff --git a/pubspec.yaml b/pubspec.yaml index df15254..21c90cb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: linalg description: A Simple Linear Algebra Package enabling easy Matrix calculations. -version: 0.1.1 +version: 0.2.0 author: Ron Bessems homepage: https://github.com/altera2015/linalg diff --git a/test/vector_test.dart b/test/vector_test.dart index 82503ca..6b782c0 100644 --- a/test/vector_test.dart +++ b/test/vector_test.dart @@ -56,6 +56,22 @@ void main() { expect(c.magnitude(), sqrt(14)); }); + test("norm", () { + final Vector c = Vector.column([1.0, 2.0, 3.0]); + expect(c.norm(), sqrt(14)); + expect(c.manhattanNorm(), 6.0); + }); + + test("sum", () { + final Vector c = Vector.column([1.0, -1.0, 1.0]); + expect(c.sum(), 1.0); + }); + + test("mean", () { + final Vector c = Vector.column([1.0, -1.0, 1.0]); + expect(c.mean(), 1.0 / 3.0); + }); + test("transpose", () { final Vector c = Vector.column([1.0, 2.0, 3.0]); final Vector tc = c.transpose();