Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpimath] Make Vector-Vector binary operators return Vector #5772

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions wpimath/src/main/java/edu/wpi/first/math/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package edu.wpi.first.math;

import edu.wpi.first.math.numbers.N1;
import java.util.Objects;
import org.ejml.simple.SimpleMatrix;

/**
Expand Down Expand Up @@ -62,6 +63,26 @@ public Vector<R> div(double value) {
return new Vector<>(this.m_storage.divide(value));
}

/**
* Adds the given vector to this vector.
*
* @param value The vector to add.
* @return The resultant vector.
*/
public final Vector<R> plus(Vector<R> value) {
return new Vector<>(this.m_storage.plus(Objects.requireNonNull(value).m_storage));
}

/**
* Subtracts the given vector to this vector.
*
* @param value The vector to add.
* @return The resultant vector.
*/
public final Vector<R> minus(Vector<R> value) {
return new Vector<>(this.m_storage.minus(Objects.requireNonNull(value).m_storage));
}

/**
* Returns the dot product of this vector with another.
*
Expand Down
38 changes: 38 additions & 0 deletions wpimath/src/test/java/edu/wpi/first/math/VectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@
import org.junit.jupiter.api.Test;

class VectorTest {
@Test
void testVectorPlus() {
var vec1 = VecBuilder.fill(1.0, 2.0, 3.0);
var vec2 = VecBuilder.fill(4.0, 5.0, 6.0);
var result1 = vec1.plus(vec2);

assertEquals(5.0, result1.get(0, 0));
assertEquals(7.0, result1.get(1, 0));
assertEquals(9.0, result1.get(2, 0));

var vec3 = VecBuilder.fill(-1.0, 2.0, -3.0);
var vec4 = VecBuilder.fill(4.0, -5.0, 6.0);
var result2 = vec3.plus(vec4);

assertEquals(3.0, result2.get(0, 0));
assertEquals(-3.0, result2.get(1, 0));
assertEquals(3.0, result2.get(2, 0));
}

@Test
void testVectorMinus() {
var vec1 = VecBuilder.fill(1.0, 2.0, 3.0);
var vec2 = VecBuilder.fill(4.0, 5.0, 6.0);
var result1 = vec1.minus(vec2);

assertEquals(-3.0, result1.get(0, 0));
assertEquals(-3.0, result1.get(1, 0));
assertEquals(-3.0, result1.get(2, 0));

var vec3 = VecBuilder.fill(-1.0, 2.0, -3.0);
var vec4 = VecBuilder.fill(4.0, -5.0, 6.0);
var result2 = vec3.minus(vec4);

assertEquals(-5.0, result2.get(0, 0));
assertEquals(7.0, result2.get(1, 0));
assertEquals(-9.0, result2.get(2, 0));
}

@Test
void testVectorDot() {
var vec1 = VecBuilder.fill(1.0, 2.0, 3.0);
Expand Down
Loading