Skip to content

Commit

Permalink
basic implement of prime factorization
Browse files Browse the repository at this point in the history
  • Loading branch information
0382 committed Dec 27, 2024
1 parent c8891b7 commit 7d4ee97
Show file tree
Hide file tree
Showing 12 changed files with 1,297 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ temp*
*.back
.ipynb_checkpoints*
build/
Manifest.toml
Manifest.toml
temp/
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CGcoefficient"
uuid = "c862aa61-d51e-47d1-b396-b1e789b4e0b6"
authors = ["jinshaoliang <[email protected]>"]
version = "0.2.11"
version = "0.3.0-alpha"

[deps]
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ makedocs(
"Wigner Symbols" => "wigner.md",
"LS-jj recoupling" => "lsjj.md",
"Moshinsky" => "moshinsky.md",
"Prime Factorization" => "pfrational.md",
],
]
)
Expand Down
24 changes: 24 additions & 0 deletions docs/src/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions docs/src/pfrational.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Prime Factorization representation of Rational

For any positive integer $N \in \mathbb{Z}^+$, it have a unique prime factorization representation
```math
N = \prod_{i}p_i^{e_i},
```
where $i = 1, 2, \dots, \infty$, $p_i$ are primes, and $e_i \ge 0$. Actually, if we modify this theorem a little, we have the prime factorization representation of rationals:

For any positive rational $q \in \mathbb{Q}^+$, it have a unique representation
```math
q = \prod_{i}p_i^{e_i},
```
where $i = 1, 2, \dots, \infty$ and $e_i \in \mathbb{N}$ can be positve, negative or zero.

Using this representation, we can quickly evaluate $q_1 \cdot q_2$ and $q_1 / q_2$:
```math
q_1 = \prod_{i}p_i^{e_i(1)}, \quad q_2 = \prod_{i}p_i^{e_i(2)}, \\
```
```math
q_1\cdot q_2 = \prod_{i}p_i^{e_i(1)+e_i(2)}, \quad q_1\cdot q_2 = \prod_{i}p_i^{e_i(1)-e_i(2)}.
```

We can also define
```math
\mathrm{gcd}(q_1, q_2) = \prod_{i}p_i^{\min\{e_i(1), e_i(2)\}}, \quad \mathrm{lcm}(q_1, q_2) = \prod_{i}p_i^{\max\{e_i(1),e_i(2)\}}
```

The prime factorization representation is very suitable for computing the factorial $n!$ and binomial $\tbinom{n}{m}$. If we known the maximum $n$ in all of the calculations, then the maximum $p_i$ needed in the calculation satisfy $p_i \le n \lt p_{i+1}$.

However, in our calculation of Wigner-3nj symbols, we need calculate some addition and subtraction of the numbers.
Considering to calculate $q_1 + q_2 - q_3$, we should first calculate
```math
q_g = gcd(q_1, q_2, q_3).
```
Then $q_1/q_g, q_2/q_g, q_3/q_g$ become integers. Next, we convert them into `BigInt` and do addition and subtraction.

## The maximum exponent

To convert the prime factorization representation of Rational into `Rational{BigInt}`, we need to store the pow table of primes to quickly obtain the result of $p_i^{e_i}$.

[Legendre's formula](https://en.wikipedia.org/wiki/Legendre%27s_formula): For any prime number $p$ and any positive integer $n$, let $\nu_{p}(n)$ be the exponent of the largest power of $p$ that divides $n$ (that is, the $p$-adic valuation of $n$). Then
```math
\nu _{p}(n!)=\sum _{i=1}^{\infty }\left\lfloor {\frac {n}{p^{i}}}\right\rfloor
```

[Kummer's Theorem](https://en.wikipedia.org/wiki/Kummer%27s_theorem): For given integers $n \ge m \ge 0$ and a prime number $p$, the p-adic valuation $\nu_{p}\!{\tbinom{n}{m}}$ of the binomial coefficient $\tbinom{n}{m}$ is equal to the number of carries when m is added to n − m in base p.

If we use factorial, then for $p^a \le n \lt p^{a-1}$, we have
```math
\nu_p(n!) = 1+p+p^2+\cdots +p^{a-1} = \frac{p^a-1}{p-1}
```
For $n = p^a, p = 2$, the maximum exponent needed is $n - 1$.

If we use the binomial, we have
```math
\sup_{0\le k\le n} \nu_p(C_n^k) \le \lfloor log_p n \rfloor,
```
because the number of carries is never larger than the number of digits of the number $n$ in base $p$.

For $n = p^a, p = 2$, the maximum exponent needed is $\lfloor log_2 n \rfloor$.
4 changes: 4 additions & 0 deletions src/CGcoefficient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ include("SqrtRational.jl")
include("util.jl")
include("WignerSymbols.jl")
include("floatWignerSymbols.jl")
include("PFRational/primetable.jl")
include("PFRational/PFRational.jl")
include("PFRational/pf_wigner.jl")
export PFRational, wigner_init_pf, pf_binomial, eCG, efCG, e3j, ef3j, e6j, ef6j

end # module CGcoefficient
Loading

0 comments on commit 7d4ee97

Please sign in to comment.