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

Improve cosine distance #1199

Merged
merged 4 commits into from
Dec 1, 2024
Merged

Improve cosine distance #1199

merged 4 commits into from
Dec 1, 2024

Conversation

rocky
Copy link
Member

@rocky rocky commented Dec 1, 2024

Handle CosineDistance more correctly in symbolic computations that should use Conjugate. Code adapted from https://github.com/axkr/symja_android_library/blob/master/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/ClusteringFunctions.java#L377-L386

However, we also add the situation where arguments are numeric scalars, and we check for mismatched vector lengths.

Some tweaking of code for conversion to/from Complex has been done.

TODO: more tests.

There is a bit more that could be done, but this is getting large already.

Fixes #1197

@rocky rocky force-pushed the improve-CosineDistance branch from 0e55da8 to ed2d3eb Compare December 1, 2024 21:55
Copy link
Contributor

@mmatera mmatera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Doctests would be OK by now. Then we can add more pytests for the module.

@rocky rocky marked this pull request as ready for review December 1, 2024 23:38
@rocky
Copy link
Member Author

rocky commented Dec 1, 2024

LGTM Doctests would be OK by now. Then we can add more pytests for the module.

Ok. Merging then.

FYI - Notes for later have:

CosineDistance[5, 6]
 = 0

CosineDistance[5, 6.0]
 = 0.0

CosineDistance[{Complex[1, 0], Complex, 0, 2]]
 = 0.0

CosineDistance[Complex[5.0, 0], Complex[10,3]]

 = 0.0421737 + 0.287348 I

CosineDistance[I, 3]
1. - 1. I

Testing that from/to python respects integerness of real and imaginary parts.
check_ArrayQ and check_SparseArrayQ should be called eval_xxx and put in mathics.eval.

I am surprised at how often when I look at the code, I see there is still very much that needs revision.

The big thing right now is that I suspect there are other things like this:

            return Expression(
                SymbolDivide,
                Expression(SymbolTotal, Expression(SymbolAbs, t)),
                Expression(
                    SymbolTotal, Expression(SymbolAbs, Expression(SymbolPlus, u, v))
                ),

which was is addressed here.

Remember in one recent issue where you mentioned a "limitation" of SymPy in that it doesn't see the full picture in order to be able to do the right thing? Well, when we write stuff like this instead of converting things to sympy and then in one go issuing:

        distance = 1 - u_val * v_val.conjugate() / (abs(u_val) * abs(v_val))

we are bringing on this kind of problem on ourselves.

@rocky rocky merged commit a039f10 into master Dec 1, 2024
13 checks passed
@rocky rocky deleted the improve-CosineDistance branch December 1, 2024 23:50
@rocky rocky restored the improve-CosineDistance branch December 2, 2024 10:19
@rocky rocky deleted the improve-CosineDistance branch December 2, 2024 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CosineDistance[{a, b}, {x, y}] - x,y values aren't conjugated
2 participants