Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
proximity
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mathis "what could possibly go wrong" Randl
proximity
Commits
c664709e
Commit
c664709e
authored
4 months ago
by
Mathis "what could possibly go wrong" Randl
Browse files
Options
Downloads
Patches
Plain Diff
l2 norm
parent
8dd759db
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/linalg/mod.rs
+1
-0
1 addition, 0 deletions
src/linalg/mod.rs
src/linalg/vector.rs
+52
-0
52 additions, 0 deletions
src/linalg/vector.rs
src/main.rs
+3
-0
3 additions, 0 deletions
src/main.rs
with
56 additions
and
0 deletions
src/linalg/mod.rs
0 → 100644
+
1
−
0
View file @
c664709e
mod
vector
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/linalg/vector.rs
0 → 100644
+
52
−
0
View file @
c664709e
pub
struct
Vector
<
'a
>
{
_repr
:
&
'a
[
f32
],
}
impl
<
'a
>
Vector
<
'a
>
{
pub
fn
len
(
&
self
)
->
usize
{
self
._repr
.len
()
}
pub
fn
l2_sim
(
&
self
,
other
:
&
Vector
<
'a
>
)
->
f32
{
debug_assert!
(
self
.len
()
==
other
.len
());
self
._repr
.iter
()
.zip
(
other
._repr
)
.map
(|(
x
,
y
)|
{
let
diff
=
x
-
y
;
diff
*
diff
})
.sum
::
<
f32
>
()
.sqrt
()
}
}
impl
<
'a
>
From
<&
'a
[
f32
]
>
for
Vector
<
'a
>
{
fn
from
(
value
:
&
'a
[
f32
])
->
Self
{
Vector
{
_repr
:
value
}
}
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
const
TOLERANCE
:
f32
=
1e-8
;
fn
close
(
actual
:
f32
,
target
:
f32
)
->
bool
{
(
target
-
actual
)
.abs
()
<
TOLERANCE
}
fn
unsuspicious_l2
(
suspect
:
f32
)
->
bool
{
suspect
.is_finite
()
&&
suspect
>=
0.0
}
#[test]
fn
self_sim_is_zero
()
{
let
testvec
=
Vector
::
from
([
1.0
,
2.0
,
3.0
,
1.5
]
.as_slice
());
let
selfsim
=
testvec
.l2_sim
(
&
testvec
);
assert!
(
unsuspicious_l2
(
selfsim
));
assert!
(
close
(
selfsim
,
0.0
));
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
3
−
0
View file @
c664709e
mod
linalg
;
fn
main
()
{
println!
(
"Hello, world!"
);
}
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