Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Simox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Leander Singer
Simox
Commits
780be114
Commit
780be114
authored
4 years ago
by
Christian Dreher
Browse files
Options
Downloads
Patches
Plain Diff
Deal with possible numerical inaccuracies.
parent
1798eb1b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
SimoxUtility/math/vector_similarity/angular_similarity.h
+6
-3
6 additions, 3 deletions
SimoxUtility/math/vector_similarity/angular_similarity.h
SimoxUtility/math/vector_similarity/cosine_similarity.h
+4
-1
4 additions, 1 deletion
SimoxUtility/math/vector_similarity/cosine_similarity.h
with
10 additions
and
4 deletions
SimoxUtility/math/vector_similarity/angular_similarity.h
+
6
−
3
View file @
780be114
...
...
@@ -30,10 +30,13 @@ namespace simox::math
angular_similarity
(
const
Eigen
::
Matrix
<
float
,
rows
,
1
>&
v1
,
const
Eigen
::
Matrix
<
float
,
rows
,
1
>&
v2
)
noexcept
{
const
float
angular_distance
=
std
::
acos
(
cosine_similarity
(
v1
,
v2
))
/
M_PI
;
return
1.
f
-
angular_distance
;
const
float
cosine_similarity
=
math
::
cosine_similarity
(
v1
,
v2
);
const
float
angular_distance
=
std
::
acos
(
cosine_similarity
)
/
M_PI
;
const
float
angular_similarity
=
1.
f
-
angular_distance
;
// Clamp to deal with numerical inaccuracies.
return
std
::
clamp
(
angular_similarity
,
0.
f
,
1.
f
);
}
}
This diff is collapsed.
Click to expand it.
SimoxUtility/math/vector_similarity/cosine_similarity.h
+
4
−
1
View file @
780be114
...
...
@@ -34,7 +34,10 @@ namespace simox::math
throw
std
::
logic_error
{
"Cosine similarity is not defined on operands which are zero."
};
}
return
v1
.
dot
(
v2
)
/
(
v1
.
norm
()
*
v2
.
norm
());
const
float
cosine_similarity
=
v1
.
dot
(
v2
)
/
(
v1
.
norm
()
*
v2
.
norm
());
// Clamp to deal with numerical inaccuracies.
return
std
::
clamp
(
cosine_similarity
,
-
1.
f
,
1.
f
);
}
}
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