How do you express $\nu_2(n)$ as a SymPy expression so that SymPy's simplification algorithm supports it?

45 Views Asked by At

$\nu_2(n)$ is the completely additive arithmetic function that is the maximum power $k$ such that $2^k \mid n$.

How do I use SymPy to represent this function. I can calculate it by hand doing:

def v_2(n:int) -> int:
   k = 0
   while n % 2 == 0:
       n /= 2
       k += 1
   return k

However, I don't think this will be compatible with SymPy's expression trees, which have rearrangement and simplification capabilities. In other words Sympy can't unwind that code and determine that $X^{v_2(4Y)} /X^{v_2(Y)}$ can be simplified to $X^2$.

So I want SymPy to know how to simplify even under the property $\nu_2(nm) = \nu_2(n) + \nu_2(m)$.

Is this possible or do I need to look into another CAS that supports it?

1

There are 1 best solutions below

1
On BEST ANSWER

According to the documentation (https://docs.sympy.org/latest/index.html), especially the expand_log (https://docs.sympy.org/latest/tutorial/simplification.html#expand-log), we can see a similar existing simplification on the logarithm.

Therefore, we at least can extend log and expand-log to implement $\nu_2$.