Skip to content
Snippets Groups Projects
Commit b8af75a7 authored by tanguy.dietrich's avatar tanguy.dietrich
Browse files
parents fefbcae7 ae2af4d5
No related branches found
No related tags found
2 merge requests!2Tanguy,!1David
This commit is part of merge request !2. Comments created here will be created in the context of that merge request.
# Numeric # Numeric
Ce repository contient l'énoncé du TP "numeric" et un squelette maven. Ce repository contient l'énoncé du TP "numeric" et un squelette maven.
Nous vous conseillons de vous abonner aux notifications (watch) pour ne pas manquer des annonces ou des changements. Nous vous conseillons de vous abonner aux notifications (watch) pour ne pas manquer des annonces ou des changements.
...@@ -8,6 +10,98 @@ Après avoir choisi votre binôme, vous devrez **impérativement** : ...@@ -8,6 +10,98 @@ Après avoir choisi votre binôme, vous devrez **impérativement** :
- Ajouter Joel Cavat (@joel.cavat), Jeremy Gobet (@jeremy.gobet) et Steven Liatti (@steven.liatti) en tant que *Reporter* de votre repository. - Ajouter Joel Cavat (@joel.cavat), Jeremy Gobet (@jeremy.gobet) et Steven Liatti (@steven.liatti) en tant que *Reporter* de votre repository.
- Lire attentivement l'énoncé. - Lire attentivement l'énoncé.
## Fonctionnalités à réaliser sur les vecteurs (itération 1)
Le projet contient déjà un ensemble de tests unitaires. Faites-en sorte que le projet compile et que les tests passsent.
Vous devez réaliser des fonctionnalités sur les calculs vectoriels (dans un premier temps). Utilisez le package `ch.hepia.numeric` pour mettre vos classes.
### Opérations sur les vecteurs (méthodes d'instance)
(Regardez les tests unitaires pour comprendre comment elles fonctionnent)
- [ ] `add(), mul(), sub()` pour les opérations de bases
- [ ] `norm()` pour calculer la norme d'un vecteur
- [ ] `t()` pour retourner la vesion transposée
- [ ] `dot()` pour retourner le produit scalaire de deux vecteurs (en notation matricielle)
- [ ] `get()` pour retourner un élément (le premier élément se trouve en position 0)
- [ ] `set()` en tant que méthode non publique pour modifier une valeur
- [ ] `map()` pour retourner une version transformée
- [ ] `concat()` pour concatener deux vecteurs
- [ ] `copy()` pour copier un vecteur
- [ ] `slice(), sliceTo(), sliceFrom()` pour les opérations de slicing
- [ ] `removed()` pour retourner une sans un des éléments
- [ ] `toString()` et `equals()` pour une représentation et la comparaison de vecteur
- [ ] Toutes ces fonctionnalités doivent exister sur un **vecteur transposé**.
- [ ] Retourne RuntimeException() avec un message approprié en cas d'erreurs
##### Vecteur
```math
\vec{x} = \begin{bmatrix}
x_1 \\ x_2 \\ ... \\ x_n
\end{bmatrix}
```
##### Transposée
```math
\vec{x}^t = \begin{bmatrix}
x_1 & x_2 & ... & x_n
\end{bmatrix}
```
##### Produit scalaire en notation matricielle
```math
\vec{x}^t \cdot \vec{y} = \begin{bmatrix}
x_1 & x_2 & ... & x_n
\end{bmatrix} \cdot \begin{bmatrix}
y_1 \\ y_2 \\ ... \\ y_n
\end{bmatrix} = \sum_{i=1}^n x_i \cdot y_i
```
##### Norme d'un vecteur
```math
\vec{v} \begin{pmatrix}
v_1 \\
v_2 \\
... \\
v_n
\end{pmatrix} = \lVert \vec{v} \rVert = \sqrt{v_1^2 + v_2^2 + ... + v_n^2}
```
##### Map
```math
map(\vec{x}, f) = \begin{bmatrix}
f(x_1) \\ f(x_2) \\ ... \\ f(x_n)
\end{bmatrix}
```
### Opérations statiques (méthodes de classes)
(Regardez les tests unitaires pour comprendre comment elles fonctionnent)
- [ ] `of()`
- [ ] `empty()`
- [ ] `fill()`
- [ ] `zeros()`
- [ ] `ones()`
- [ ] `linespace()`
- [ ] `tabulate()`
- [ ] `sum()`
- [ ] `norms()`
### Remarques
Les constructeurs doivent être non publiques. Pour créer un élément, vous devez utilisez une fabrique (méthode statique) tel que `of()` ou `empty()`
## Fonctionnalités à réaliser sur les matrices (prochaine itération)
TBD
# Maven # Maven
...@@ -29,7 +123,7 @@ mvn package ...@@ -29,7 +123,7 @@ mvn package
java -cp target/my-app-0.1.jar ch.hepia.my_app.App java -cp target/my-app-0.1.jar ch.hepia.my_app.App
``` ```
- ou, à l'aide du plugin `exec-maven-plugin`: - ou, à l'aide du plugin `exec-maven-plugin` (plus simple):
``` ```
mvn exec:java mvn exec:java
......
package ch.hepia.my_app; package ch.hepia;
/** /**
* Hello world! * Hello world!
......
package ch.hepia.numeric;
import java.util.List;
import java.util.function.DoubleFunction;
import java.util.function.Function;
final public class Vector {
private Vector() {}
private Vector(Double... elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
private Vector(List<Double> elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Transposed t() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public int len() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public double get(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
void set(int i, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector add(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector mul(double m) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector sub(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public double norm() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector sliceFrom(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector sliceTo(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector slice(int from, int to) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector removed(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector concat(Vector that) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector map(DoubleFunction<Double> f) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Vector copy() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
void checkVectorLengthOrThrow(Vector that){
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector of(Double... elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector of(List<Double> elements) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector empty() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector fill(int nb, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector zeros(int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector ones(int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector linespace(double from, double to, int nb) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector tabulate(int nb, Function<Integer, Double> f) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Vector sum(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static double norms(List<Vector> vs) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
@Override
public String toString() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
@Override
public boolean equals(Object obj) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
}
package ch.hepia.my_app;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* Unit test for simple App.
*/
class AppTest {
@Test
void dummyTest() {
assertTrue(true);
}
}
package ch.hepia.numeric;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
class VectorTest {
@Test
void vectorMainOperations() {
Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = Vector.of(-1.0, -2.0, -3.0);
assertEquals(v1.add(v2), Vector.of(0.0, 0.0, 0.0));
assertEquals(v1.sub(v2), Vector.of(2.0, 4.0, 6.0));
assertEquals(v1.mul(3.0), Vector.of(3.0, 6.0, 9.0));
assertEquals(Vector.empty().len(), 0);
assertEquals(Vector.of(1.0, 2.0, 1.0).len(), 3);
}
@Test
void vectorCreation() {
assertEquals(Vector.zeros(3), Vector.of(0.0, 0.0, 0.0));
assertEquals(Vector.ones(3), Vector.of(1.0, 1.0, 1.0));
assertEquals(Vector.of(1.0, 2.0).map( d -> d * 2.0), Vector.of(2.0, 4.0));
assertEquals(Vector.fill(3, 1.7), Vector.of(1.7, 1.7, 1.7));
assertEquals(Vector.tabulate(4, i -> i*5.0), Vector.of(0.0, 5.0, 10.0, 15.0));
}
@Test
void vectorLinespace() {
assertEquals(Vector.linespace(0.0, 1.0, 3), Vector.of(0.0, 0.5, 1.0));
assertEquals(Vector.linespace(0.0, 1.0, 5), Vector.of(0.0, 0.25, 0.5, 0.75, 1.0));
}
@Test
void vectorNormAndSum() {
assertEquals(Vector.of(1.0, 2.0, 2.0).norm(), 3.0);
assertEquals(Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))).norm(), 3.0);
assertEquals(
Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))).norm(),
Vector.norms(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0)))
);
assertEquals(Vector.sum(List.of(Vector.of(1.0, 2.0, 1.0), Vector.of(0.0, 0.0, 1.0))), Vector.of(1.0, 2.0, 2.0));
}
@Test
void vectorTranspose() {
Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = Vector.of(1.0, 0.0, -1.0);
assertEquals(v1.t().dot(v2), -2.0);
/*
* v1.dot(v2) should not compile !!!
* only a transposed vector with a vector
*/
Vector v3 = Vector.of(1.0, 2.0, 3.0);
Vector v4 = Vector.of(1.0, 2.0, 3.0);
assertEquals( v3.t().dot(v4), 14.0 );
assertEquals( v3.t().sub(v4.t()), Vector.of(0.0, 0.0, 0.0).t() );
}
@Test
void vectorToString() {
Vector v = Vector.of(1.0, 2.0, 3.0);
assertEquals(v.toString(), "Vector[1.0, 2.0, 3.0]");
assertEquals(v.t().toString(), "Transposed[1.0, 2.0, 3.0]");
}
@Test
void vectorSlice() {
Vector v = Vector.of(1.0, 2.0, 3.0, 4.0, 5.0);
assertEquals(v.sliceFrom(2), Vector.of(3.0, 4.0, 5.0));
assertEquals(v.sliceTo(3), Vector.of(1.0, 2.0, 3.0));
assertEquals(v.slice(2,4), Vector.of(3.0, 4.0));
assertEquals(v.slice(4,4), Vector.empty());
assertEquals(v.slice(4,2), Vector.empty());
assertEquals(v.removed(2), Vector.of(1.0, 2.0, 4.0, 5.0));
}
@Test
void vectorConcat() {
Vector v1 = Vector.of(1.0, 2.0, 3.0);
Vector v2 = Vector.of(-1.0, -2.0, -3.0);
assertEquals(v1.concat(v2), Vector.of(1.0, 2.0, 3.0, -1.0, -2.0, -3.0));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment