22-11-15-Functions
To conclude the functions that I met in these years.
Just show that how the functions and equations
make a one-to-one relation.
Relating to python, sagemath……
python3
normal
num.bit_length()
1 | generate_prime(10).bit_length() |
gmpy2
gcd()
input: gcd(a, b)
output: the greatest
To solve out the greatest common factor between $a$ and $b$
gcdext(a, b)
invert(a, n)
powmod(a, b, c)
next_prime(a)
iroot()
pycryptodome
bytes_to_long()
long_to_bytes()
sympy
symbol()
input: unknowns
output: defined unknowns
Define the unknowns, such as $x$, $y$, etc.
Two ways to define:
- an unknown quantity:
x = Symbol('x')
- more than one unknown quantity:
x, y = symbols('x y')
solve()
input: solve([equations], [unknowns])
output: the root of the equations
z3
install
- cmd
pip install z3_solver
- pycharm
setting -> python interpreter -> + -> search for z3_solver.eg
import
from z3 import *
import z3
Int()/Ints()
input: unknown
output: Integer variable.
eg: 3, 5, 7……
1 | a = Int('a') |
Real()/Reals()
input: unknown
output: Real variable.
eg: $2^{\frac{1}{2}}$……
1 | a = Real('a') |
Same above.
BitVec()/BitVecs()
input: bit vector unknown
output: bit vectors
eg: 11000001, 11111000……
1 | a = BitVec('a', 8) |
Solver()
create a Solver to calculate
1 | s = Solver() |
add()
input: equations or constraints
output: add to conditions
1 | # s = Solver() |
check()
to check the conditions and solve out
output: sat / unsat
1 | # s.add(equ) |
model()
solve out the satisfied roots
output: the roots
1 | # s.check() |
itertools
Traverse all possibilities
itertools — Functions creating iterators for efficient looping — Python 3.11.1 documentation
1 | from itertools import * |
product()
1 | product('ABCD', repeat = 2) |
permutations()
1 | permutations('ABCD', 2) |
combinations()
1 | combinations('ABCD', 2) |
combinations_with_replacement()
1 | combinations_with_replacement('ABCD', 2) |
libnum
1 | from libnum import * |
n2s(n)
number convert to string
s2n(s)
string convert to number
s2b(s)
string convert to binary
b2s(b)
binary convert to string
prime(n)
produce primes < = n
1 | 14) primes( |
generate_prime(n)
generate bit_length=n prime
1 | generate_prime(10) |
sagemath
var()
input: Integer
output: Integer variables
1 | x, y, z = var('x y z') |
solve()
input: solve([equations], [variables])
output: roots
1 | """ |
Q.coefficients()
The coefficients in the polynomial Q are extracted and sorted in ascending order.
1 | sage: x = var('x') |
g.monic()
In algebra, the first coefficient of a polynomial is often called the leading coefficient of the polynomial, and turning a polynomial into a form with a first coefficient of 1 is called a monic form.