Python Forum
i am wondering if squaring .... - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: i am wondering if squaring .... (/thread-13120.html)

Pages: 1 2


i am wondering if squaring .... - Skaperen - Sep-28-2018

i am wondering if squaring is faster than multiply in Python's large int arithmetic. i know there is a way, that i cannot recall, to optimize multiplication of a number with itself, mathematically. this cannot be done when the two numbers are different. i'm curious if the big int logic includes this for the case of raising power by 2 making a**2 faster than a*b where b != a but b is nearly as big as a,


RE: i am wondering if squaring .... - wavic - Sep-30-2018

In [1]: def square(a):
   ...:     a ** 2
   ...:     

In [2]: def multiply(a):
   ...:     a * a
   ...:     

In [3]: %timeit -n 1000000 square(1111)
270 ns ± 1.91 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [4]: %timeit -n 1000000 multiply(1111)
97 ns ± 0.801 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [5]: 
If you want speed go try gmpy2 module.


RE: i am wondering if squaring .... - Skaperen - Sep-30-2018

i'll take that as a "no".

actually i had a way to use squaring as an alternative to multiplying for an app. if squaring was faster, it would be worth doing things that way. apparently not. big int is faster in Pike. but I'll stay with Python.


RE: i am wondering if squaring .... - Skaperen - Oct-01-2018

if anyone is considering optimizing CPython, they should at least look at adding a check in the raise power (**) code to check for x**2 and x**3 (int powers) and do x*x an x*x*x instead of doing it the logarithm way. i doubt it is doing logarithms for big int to int power since i am getting really big int answers with all the right digits.

lt1/forums /home/forums 3> py3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**64
18446744073709551616
>>> 20000000000000000001
20000000000000000001
>>> 20000000000000000001**3
8000000000000000001200000000000000000060000000000000000001
>>>



RE: i am wondering if squaring .... - wavic - Oct-01-2018

I don't know what they are doing underneath.
I was playing with really big, big numbers ( few million digits ) and all is fine. I want to play again but this time I will try CUDA to see how it is going to run. I just wonder if uint64 could contain such a number. Or much bigger.


RE: i am wondering if squaring .... - Skaperen - Oct-02-2018

uint64 (in C) is limited to a maximum of 2**64-1. in C, you can still assign -1 to it an will get that largest possible value. gcc supports a 64 bit int on 32 bit architectures and a 128 bit int on 64 bit architectures. to go beyond that, use libgmp or switch to Python.

in C, x > 0xffffffffffffU would never be true for any possible x that is uint64.

if power takes more time than the equivalent multiply, especially for big int, then power must be doing something else or something more. maybe for small int it is using logarithms. for big powers, that is the fast way. maybe a good test would be to work out the power with logs, and compare that to straight powering.


RE: i am wondering if squaring .... - perfringo - Oct-02-2018

Loosely related:

>>> -2 ** 2
-4
>>> (-2) ** 2
4
>>> from math import pow
>>> pow(-2, 2)
4
Negative number is not literal. Negative number is unary operator and literal therefore ** takes precedence. pow works in more intuitive way.


RE: i am wondering if squaring .... - wavic - Oct-02-2018

I am asking because I saw examples where in order to use CUDA in Python I have to provide the type of the variables. I do not plan to use C but Python. But I was thinking that because of CPython there will be limitations if I have to say explicitly the types of the variables. I have to review the documentation in more detail. Anyone having fun with CUDA here? Rolleyes


RE: i am wondering if squaring .... - Skaperen - Oct-03-2018

i think Python float is limited to 64 bits and hence has a type as CUDA might know them. a really big int would be some kind of array. i'm have no idea how you might need to pass data to CUDA from Python, much less tell CUDA what it is getting. are you going to be running some specific software there that might document this or are you expecting to run your own Python code on it?


RE: i am wondering if squaring .... - wavic - Oct-03-2018

I will run my own Python code. I will stick with the big integers for now.