Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
poo2019numeric
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
francois.hofer
poo2019numeric
Commits
90019aae
Commit
90019aae
authored
5 years ago
by
fefe
Browse files
Options
Downloads
Patches
Plain Diff
Transposed functions tested
parent
4a6a79cc
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/ch/hepia/numeric/Transposed.java
+147
-2
147 additions, 2 deletions
src/main/java/ch/hepia/numeric/Transposed.java
src/test/java/ch/hepia/numeric/AppTest.java
+49
-0
49 additions, 0 deletions
src/test/java/ch/hepia/numeric/AppTest.java
with
196 additions
and
2 deletions
src/main/java/ch/hepia/numeric/Transposed.java
+
147
−
2
View file @
90019aae
package
ch.hepia.numeric
;
import
java.util.List
;
import
java.util.ArrayList
;
import
java.util.function.DoubleFunction
;
import
java.util.function.Function
;
public
class
Transposed
{
private
List
<
Double
>
vecT
;
...
...
@@ -9,6 +11,16 @@ public class Transposed{
this
.
vecT
=
new
ArrayList
<>();
};
private
Transposed
(
Double
...
elements
)
{
List
<
Double
>
vec
=
new
ArrayList
<>();
for
(
double
elem
:
elements
){
vec
.
add
(
elem
);}
this
.
vecT
=
vec
;
}
public
Transposed
(
List
<
Double
>
elements
){
this
.
vecT
=
elements
;
}
public
Transposed
(
Vector
That
){
this
.
vecT
=
new
ArrayList
<>();
for
(
double
elem
:
That
.
getValues
()){
vecT
.
add
(
elem
);}
...
...
@@ -18,6 +30,10 @@ public class Transposed{
return
this
.
vecT
;
}
void
set
(
int
i
,
double
value
)
{
this
.
vecT
.
set
(
i
,
value
);
}
public
int
len
()
{
return
this
.
getValues
().
size
();
}
...
...
@@ -35,7 +51,32 @@ public class Transposed{
}
return
res
;
//throw new UnsupportedOperationException("This feature isn't implemented yet");
};
}
public
Transposed
add
(
Transposed
that
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
this
.
len
();
i
++){
newVec
.
add
(
this
.
get
(
i
)+
that
.
get
(
i
));
}
return
new
Transposed
(
newVec
);
}
public
static
Transposed
zeros
(
int
nb
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
nb
;
i
++){
newVec
.
add
(
0.0
);
}
return
new
Transposed
(
newVec
);
}
public
static
Transposed
sum
(
List
<
Transposed
>
vs
)
{
Transposed
totalVec
=
Transposed
.
zeros
(
vs
.
get
(
0
).
len
());
for
(
Transposed
vec
:
vs
){
totalVec
=
totalVec
.
add
(
vec
);
}
return
totalVec
;
}
public
Transposed
sub
(
Transposed
That
){
Transposed
resT
=
new
Transposed
();
Integer
count
=
0
;
...
...
@@ -47,7 +88,111 @@ public class Transposed{
//throw new UnsupportedOperationException("This feature isn't implemented yet");
};
@Override
public
Transposed
mul
(
double
m
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
this
.
len
();
i
++){
newVec
.
add
(
this
.
get
(
i
)*
m
);
}
return
new
Transposed
(
newVec
);
}
public
static
Transposed
of
(
Double
...
elements
)
{
return
new
Transposed
(
elements
);
}
public
static
Transposed
of
(
List
<
Double
>
elements
)
{
return
new
Transposed
(
elements
);
}
public
static
Transposed
empty
()
{
return
new
Transposed
();
}
public
double
norm
()
{
double
sum
=
0.0
;
for
(
int
i
=
0
;
i
<
this
.
len
();
i
++){
sum
+=
Math
.
pow
(
this
.
get
(
i
),
2.0
);
}
return
Math
.
sqrt
(
sum
);
}
public
static
double
norms
(
List
<
Transposed
>
vs
)
{
return
Transposed
.
sum
(
vs
).
norm
();
}
public
Transposed
map
(
DoubleFunction
<
Double
>
f
)
{
List
<
Double
>
accVec
=
this
.
getValues
();
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
Double
val
:
accVec
){
newVec
.
add
(
f
.
apply
(
val
));
}
return
new
Transposed
(
newVec
);
}
public
static
Transposed
ones
(
int
nb
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
nb
;
i
++){
newVec
.
add
(
1.0
);
}
return
new
Transposed
(
newVec
);
}
public
static
Transposed
fill
(
int
nb
,
double
value
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
nb
;
i
++){
newVec
.
add
(
value
);
}
return
new
Transposed
(
newVec
);
}
public
static
Transposed
tabulate
(
int
nb
,
Function
<
Integer
,
Double
>
f
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
nb
;
i
++){
newVec
.
add
(
f
.
apply
(
i
));
}
return
new
Transposed
(
newVec
);
}
public
Transposed
concat
(
Transposed
that
)
{
List
<
Double
>
newVec
=
this
.
getValues
();
newVec
.
addAll
(
that
.
getValues
());
return
new
Transposed
(
newVec
);
}
public
Transposed
sliceFrom
(
int
i
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
j
=
0
;
j
<
this
.
len
();
j
++){
if
(
j
>=
i
){
newVec
.
add
(
this
.
get
(
j
));
}
}
return
new
Transposed
(
newVec
);
}
public
Transposed
sliceTo
(
int
i
)
{
List
<
Double
>
newVec
=
new
ArrayList
<>();
for
(
int
j
=
0
;
j
<
this
.
len
();
j
++){
if
(
j
<
i
){
newVec
.
add
(
this
.
get
(
j
));
}
}
return
new
Transposed
(
newVec
);
}
public
Transposed
slice
(
int
from
,
int
to
)
{
Transposed
newVec
=
this
;
newVec
=
newVec
.
sliceFrom
(
from
);
newVec
=
newVec
.
sliceTo
(
to
-
from
);
return
newVec
;
}
public
Transposed
removed
(
int
i
)
{
List
<
Double
>
newVec
=
this
.
getValues
();
newVec
.
remove
(
i
);
return
new
Transposed
(
newVec
);
}
@Override
public
String
toString
()
{
String
tr
=
"Transposed["
;
for
(
double
val:
this
.
getValues
()){
...
...
This diff is collapsed.
Click to expand it.
src/test/java/ch/hepia/numeric/AppTest.java
+
49
−
0
View file @
90019aae
...
...
@@ -88,4 +88,53 @@ class VectorTest {
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
));
}
// Test Transposed :
@Test
void
transposedMainOp
()
{
Transposed
v1
=
Transposed
.
of
(
1.0
,
2.0
,
3.0
);
Transposed
v2
=
Transposed
.
of
(-
1.0
,
-
2.0
,
-
3.0
);
assertEquals
(
v1
.
add
(
v2
),
Transposed
.
of
(
0.0
,
0.0
,
0.0
));
assertEquals
(
v1
.
sub
(
v2
),
Transposed
.
of
(
2.0
,
4.0
,
6.0
));
assertEquals
(
v1
.
mul
(
3.0
),
Transposed
.
of
(
3.0
,
6.0
,
9.0
));
assertEquals
(
Transposed
.
empty
().
len
(),
0
);
assertEquals
(
Transposed
.
of
(
1.0
,
2.0
,
1.0
).
len
(),
3
);
}
@Test
void
transposedNormAndSum
()
{
assertEquals
(
Transposed
.
of
(
1.0
,
2.0
,
2.0
).
norm
(),
3.0
);
assertEquals
(
Transposed
.
sum
(
List
.
of
(
Transposed
.
of
(
1.0
,
2.0
,
1.0
),
Transposed
.
of
(
0.0
,
0.0
,
1.0
))).
norm
(),
3.0
);
assertEquals
(
Transposed
.
sum
(
List
.
of
(
Transposed
.
of
(
1.0
,
2.0
,
1.0
),
Transposed
.
of
(
0.0
,
0.0
,
1.0
))).
norm
(),
Transposed
.
norms
(
List
.
of
(
Transposed
.
of
(
1.0
,
2.0
,
1.0
),
Transposed
.
of
(
0.0
,
0.0
,
1.0
)))
);
assertEquals
(
Transposed
.
sum
(
List
.
of
(
Transposed
.
of
(
1.0
,
2.0
,
1.0
),
Transposed
.
of
(
0.0
,
0.0
,
1.0
))),
Transposed
.
of
(
1.0
,
2.0
,
2.0
));
}
@Test
void
transposedCreation
()
{
assertEquals
(
Transposed
.
zeros
(
3
),
Transposed
.
of
(
0.0
,
0.0
,
0.0
));
assertEquals
(
Transposed
.
ones
(
3
),
Transposed
.
of
(
1.0
,
1.0
,
1.0
));
assertEquals
(
Transposed
.
of
(
1.0
,
2.0
).
map
(
d
->
d
*
2.0
),
Transposed
.
of
(
2.0
,
4.0
));
assertEquals
(
Transposed
.
fill
(
3
,
1.7
),
Transposed
.
of
(
1.7
,
1.7
,
1.7
));
assertEquals
(
Transposed
.
tabulate
(
4
,
i
->
i
*
5.0
),
Transposed
.
of
(
0.0
,
5.0
,
10.0
,
15.0
));
}
@Test
void
transposedConcat
()
{
Transposed
v1
=
Transposed
.
of
(
1.0
,
2.0
,
3.0
);
Transposed
v2
=
Transposed
.
of
(-
1.0
,
-
2.0
,
-
3.0
);
assertEquals
(
v1
.
concat
(
v2
),
Transposed
.
of
(
1.0
,
2.0
,
3.0
,
-
1.0
,
-
2.0
,
-
3.0
));
}
@Test
void
transposedSlice
()
{
Transposed
v
=
Transposed
.
of
(
1.0
,
2.0
,
3.0
,
4.0
,
5.0
);
assertEquals
(
v
.
sliceFrom
(
2
),
Transposed
.
of
(
3.0
,
4.0
,
5.0
));
assertEquals
(
v
.
sliceTo
(
3
),
Transposed
.
of
(
1.0
,
2.0
,
3.0
));
assertEquals
(
v
.
slice
(
2
,
4
),
Transposed
.
of
(
3.0
,
4.0
));
assertEquals
(
v
.
slice
(
4
,
4
),
Transposed
.
empty
());
assertEquals
(
v
.
slice
(
4
,
2
),
Transposed
.
empty
());
assertEquals
(
v
.
removed
(
2
),
Transposed
.
of
(
1.0
,
2.0
,
4.0
,
5.0
));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment