1
$\begingroup$

Is it possible to use Gwnum library to accelerate the multiplication in Mathematica?

$\endgroup$
  • 3
    $\begingroup$ This appears to be a C library and yes, they can be principally called through LibraryLink. It is not easy though and it also requires a bit of C programming on your sid. $\endgroup$ – Henrik Schumacher Jul 24 at 17:08
  • 2
    $\begingroup$ To be clear, it won't be possible to override the default multiplication operator (but yes you can call any C library through LibraryLink if you take the time to write the glue code). $\endgroup$ – Szabolcs Jul 24 at 17:46
  • 2
    $\begingroup$ One problem may be that Mathematica does not have a good API to access bignums directly. You must serialize them some way before passing them to a C program. You could convert them to a string or use IntegerDigits e.g. in base 256 or similar. This serialization, and the overhead from calling a LibraryLink function, may take more time than the multiplication itself (unless the integers are huge). $\endgroup$ – Szabolcs Jul 24 at 20:01
  • 5
    $\begingroup$ Actually, a quick benchmark seems to show that IntegerDigits takes longer than Mathematica's native multiplication ... Do[IntegerDigits[123456789123456789123456789123456789, 65536], {1000}] // RepeatedTiming vs Do[123456789123456789123456789123456789* 123456789123456789123456789123456789, {1000}] // RepeatedTiming $\endgroup$ – Szabolcs Jul 24 at 20:04
  • 2
    $\begingroup$ This reads like 2 questions to me: 1 Q: Is it possible to call the Gwnum library from Mathematica? A: Probably, with a bit of C programming to glue the two together. And, more interesting perhaps 2 Q Will using Gwnum accelerate multiplication ? A: As a paying customer I'd be a bit miffed to find that it did ! $\endgroup$ – High Performance Mark Jul 27 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.