logo
down
shadow

Cosine Calculation without cmath library


Cosine Calculation without cmath library

Content Index :

Cosine Calculation without cmath library
Tag : cpp , By : Rohii
Date : November 25 2020, 01:01 AM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

most efficient R cosine calculation


Tag : performance , By : Franky
Date : March 29 2020, 07:55 AM
With these it helps All the functions you're using are .Primitive (therefore already call compiled code directly), so it will be hard to find consistent speed gains outside of re-building R with an optimized BLAS. With that said, here is one option that might be faster for larger vectors:
cosine_calc2 <- function(a,b,wts) {
  a = a*wts
  b = b*wts
  crossprod(a,b)/sqrt(crossprod(a)*crossprod(b))
}

all.equal(cosine_calc1(a,b,w),cosine_calc2(a,b,w))
# [1] TRUE

# Check some timings
library(rbenchmark)
# cosine_calc2 is slower on my machine in this case
benchmark(
  cosine_calc1(a,b,w),
  cosine_calc2(a,b,w), replications=1e5, columns=1:4 )
#                    test replications user.self sys.self
# 1 cosine_calc1(a, b, w)       100000      1.06     0.02
# 2 cosine_calc2(a, b, w)       100000      1.21     0.00

# but cosine_calc2 is faster for larger vectors
set.seed(21)
a <- rnorm(1000)
b <- rnorm(1000)
w <- runif(1000)
benchmark(
  cosine_calc1(a,b,w),
  cosine_calc2(a,b,w), replications=1e5, columns=1:4 )
#                    test replications user.self sys.self
# 1 cosine_calc1(a, b, w)       100000      3.83        0
# 2 cosine_calc2(a, b, w)       100000      2.12        0
> Rprof(); for(i in 1:100000) cosine_calc2(a,b,w); Rprof(NULL); summaryRprof()
$by.self
             self.time self.pct total.time total.pct
*                 0.80    45.98       0.80     45.98
crossprod         0.56    32.18       0.56     32.18
cosine_calc2      0.32    18.39       1.74    100.00
sqrt              0.06     3.45       0.06      3.45

$by.total
             total.time total.pct self.time self.pct
cosine_calc2       1.74    100.00      0.32    18.39
*                  0.80     45.98      0.80    45.98
crossprod          0.56     32.18      0.56    32.18
sqrt               0.06      3.45      0.06     3.45

$sample.interval
[1] 0.02

$sampling.time
[1] 1.74
cosine_calc3 <- function(a,b) {
  crossprod(a,b)/sqrt(crossprod(a)*crossprod(b))
}
A = a*w
B = b*w
# Run again on the 1000-element vectors
benchmark(
  cosine_calc1(a,b,w),
  cosine_calc2(a,b,w),
  cosine_calc3(A,B), replications=1e5, columns=1:4 )
#                    test replications user.self sys.self
# 1 cosine_calc1(a, b, w)       100000      3.85     0.00
# 2 cosine_calc2(a, b, w)       100000      2.13     0.02
# 3    cosine_calc3(A, B)       100000      1.31     0.00

Calculation sine and cosine in one shot


Tag : cpp , By : user184975
Date : March 29 2020, 07:55 AM
I wish this help you If you seek fast evaluation with good (but not high) accuracy with powerseries you should use an expansion in Chebyshev polynomials: tabulate the coefficients (you'll need VERY few for 0.1% accuracy) and evaluate the expansion with the recursion relations for these polynomials (it's really very easy).
References:

Efficient calculation of cosine in python


Tag : python , By : Tigre
Date : March 29 2020, 07:55 AM

Efficient cosine distance calculation


Tag : python , By : S. Fenz
Date : March 29 2020, 07:55 AM

Cosine similarity calculation between two matrices


Tag : python , By : Mikael
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How to copy QString into wchar_t buffer
  • Make the compiler generate an empty default function for an std::function
  • Insert or push_back to end of a std::vector?
  • Best practice for const temporary types
  • Include CSV File in Complied Binary
  • Issue with binding non static function to callback
  • How can read from some files and write content of them with different form into files with same names
  • Why does auto deduce this variable as double and not float?
  • no instance of overloaded function "std::make_unique" matches the argument list, but works with unique_ptr con
  • How to see array size from a pointer in c++
  • Error taking address of temporary in Eclipse
  • Using an iterator to go through a vector and modify the contents
  • Are extern extern "C", and extern "C" extern, allowed?
  • Can't solve C2660 and C2065 Errors
  • C referencing C++ extern
  • How to write the definition of a derived class in c++?
  • Why when I include <cmath> I need to use the namespace std too?
  • How to assign a 32-bit unsigned integer to a bit field containing 32 bits
  • Why does the same class being defined in multiple .cpp files not cause a linker multiple definition error?
  • C++ 11db error when trying to quit the program in xcode. beginner level
  • Add content of a vector into a Capnproto map object
  • Recursively Pass Template Template To a Template Template Function
  • Swap rows in a 2D array with std::swap. How does it work?
  • Is there any situation in which an object's storage might change during its lifetime?
  • clang++ always generates empty profraw coverage reports
  • Do memory leaks persist after program completion if the OS does not clear it?
  • How to link library using cmake
  • How to use getters and setters without generating a copy?
  • Generating multiple amounts of the same sprite broken
  • function in c++ why my compiler didn't recognize the error()
  • Relationship between copy(...) and copy(seq, ...)
  • Are <cmath> functions required to be `noexcept` in C++17?
  • How to find a struct list item
  • How can you handle DLL versions when referencing C++ DLL's over COM from VBScript with CreateObject?
  • Do not understand how c++ set works
  • Actual build date in C++
  • How to link to already compiled external shared libraries in RCPP using Makevars?
  • combination of enable_if + std::less + sizeof... makes MSVC fail
  • Can you call the destructor without calling the constructor?
  • How do I prevent a function from freeing memory of a local variable?
  • Why am I getting an exception with a push involved with a shared pointer?
  • Resizing an array by pointer
  • avoiding dynamic_cast without increasing coupling
  • I cannot solve the else part in my if-else ladder?
  • Inherit from arbitrary class in c++?
  • LNK2019 unresolved external symbol from MSVCRTD.lib
  • Do I need to free wchar memory allocated when using 'new' or does delete[] also free it?
  • Object creation with varying buffer size
  • Is there a way to save a variable in an std::string?
  • template lambda vs functor with template operator()
  • How to release boost::interprocess::named_mutex when the process crashes
  • How do I implement the Interface Segregation Principle using smart pointers in C++?
  • Why is my getline not reading .csv files properly?
  • size of 2d array passed into a function
  • How to properly return a unique_ptr from a vector?
  • Can I have a vector that shares storage with another vector?
  • How to create a program to replace multiple blanks/tabs by single blanks/tabs?
  • Is it allowed to assign from an object that has some indeterminate values?
  • How to dynamically allocate memory for const char double pointer (using new)
  • How to enforce derived class members on derived classes
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com