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
2
3
4
5
6
7
generate_prime(10).bit_length()
# 10
num = 1024
num.bit_length()
# 11
1024.bit_length()
# error

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

sympy.solve()方法|极客教程 (geek-docs.com)

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
2
3
a = Int('a')
# or
a, b = Ints('a b')

Real()/Reals()

input: unknown

output: Real variable.

eg: $2^{\frac{1}{2}}$……

1
2
3
a = Real('a')
# or
a, b = Reals('a b')

Same above.

BitVec()/BitVecs()

input: bit vector unknown

output: bit vectors

eg: 11000001, 11111000……

1
2
3
a = BitVec('a', 8)
# or
a, b = BitVecs('a b', 8)

Solver()

create a Solver to calculate

1
s = Solver()

add()

input: equations or constraints

output: add to conditions

1
2
3
# s = Solver()
# equ = [a + b == 10, a - b == 6]
s.add(equ)

check()

to check the conditions and solve out

output: sat / unsat

1
2
3
# s.add(equ)
s.check()
# sat

model()

solve out the satisfied roots

output: the roots

1
2
3
4
# s.check()
# if the output is sat that stands for there exist roots
s.model()
# [a = 8, b = 2]

itertools

Traverse all possibilities

itertools — Functions creating iterators for efficient looping — Python 3.11.1 documentation

1
2
from itertools import *
import itertools

product()

1
2
product('ABCD', repeat = 2)
# AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD

permutations()

1
2
3
permutations('ABCD', 2)
# AB AC AD BA BC BD CA CB CD DA DB DC
# no repeated compared to result above

combinations()

1
2
combinations('ABCD', 2)
# AB AC AD BC BD CD

combinations_with_replacement()

1
2
3
combinations_with_replacement('ABCD', 2)
# AA AB AC AD BB BC BD CC CD DD
# repeated compared to result above

libnum

1
2
from libnum import *
import libnum

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
2
3
4
>>> primes(14)
[2, 3, 5, 7, 11, 13]
>>> primes(13)
[2, 3, 5, 7, 11, 13]

generate_prime(n)

generate bit_length=n prime

1
2
generate_prime(10)
# 787

sagemath

Reference Manual (sagemath.org)

var()

input: Integer

output: Integer variables

1
x, y, z = var('x y z')

solve()

input: solve([equations], [variables])

output: roots

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""
3x-y+z=185
2x+3y-z=321
x+y+z=173
"""
x, y, z = var('x y z')
f1 = 3*x - y + z == 185
f2 = 2*x + 3*y - z == 321
f3 = x + y + z == 185
sol = solve([f1, f2, f3], [x, y, z])
# [[x == 74, y == 68, z == 31]]

"""
x*x+x-7943722218936282=0
"""
f = x*x + x - 7943722218936282
solve([f], [x])
# [x == 89127561, x == -89127562]

Q.coefficients()

The coefficients in the polynomial Q are extracted and sorted in ascending order.

1
2
3
4
5
6
7
8
sage: x = var('x')
sage: f = x^4 + 333*x^2 + 1234*x - 294832
sage: f.coefficients()
[[-294832, 0], [1234, 1], [333, 2], [1, 4]]
sage: f.coefficients()[0]
[-294832, 0]
sage: f.coefficients()[0][0]
-294832

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.

PolynomialRing?

Ideal?

-------------THE END-------------