Skip to content

Commit

Permalink
Version 0.2.0 More Vector functionality.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
altera2015 committed Dec 15, 2018
1 parent 8035b45 commit d33728c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
96 changes: 95 additions & 1 deletion lib/vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -156,14 +160,79 @@ 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];
}
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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<double>] 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<double> toList() {
if (_vectorType == VectorType.row) {
return this._matrix[0];
} else {
List<double> l = List<double>(this.elements);
for (int i = 0; i < this.elements; i++) {
l[i] = this[i];
}
return l;
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <rbessems@gmail.com>
homepage: https://github.com/altera2015/linalg

Expand Down
16 changes: 16 additions & 0 deletions test/vector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d33728c

Please sign in to comment.