# Matrix Factorials

**URL:** <https://discourse.julialang.org/t/matrix-factorials/111515>\
**Category:** Numerics\
**Created:** [March 12, 2024, 12:56pm UTC](https://discourse.julialang.org/t/matrix-factorials/111515 "2024-03-12T12:56:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZekeMarshall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekemarshall/32/206876_2.png) [@ZekeMarshall](https://discourse.julialang.org/u/ZekeMarshall)\
**Post date:** [March 12, 2024, 12:56pm UTC](https://discourse.julialang.org/t/matrix-factorials/111515/1 "2024-03-12T12:56:44Z")

</div>

Hi,

I’ve been looking for a function to calculate the factorial of a matrix, but haven’t had any luck.

I was wondering whether someone could point me in the right direction?

Apologies if i have missed something obvious!

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 12, 2024, 1:08pm UTC](https://discourse.julialang.org/t/matrix-factorials/111515/2 "2024-03-12T13:08:09Z")

</div>

> [@ZekeMarshall](#):
>
> factorial of a matrix

Are you sure you want the factorial and not the exponent? or perhaps the factorization (i.e. representation as a product of some structures matrices)?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 12, 2024, 1:52pm UTC](https://discourse.julialang.org/t/matrix-factorials/111515/3 "2024-03-12T13:52:23Z")

</div>

> [@ZekeMarshall](#):
>
> I’ve been looking for a function to calculate the factorial of a matrix

I guess you mean to extend factorial to matrices [by means of the gamma function (stackoverflow)](https://math.stackexchange.com/questions/1634488/how-could-we-define-the-factorial-of-a-matrix)?

Is your matrix symmetric positive definite? If so, you could simple apply the gamma function to the eigenvalues, e.g.

```julia
julia> using LinearAlgebra, SpecialFunctions

julia> A = randn(5,5); A = Symmetric(A'A) # random PSD matrix
5×5 Symmetric{Float64, Matrix{Float64}}:
  2.91012 -0.227897 -1.31028 -0.198762 0.375903
 -0.227897 3.00476 -3.01805 -1.45998 -1.30752
 -1.31028 -3.01805 4.35062 1.72612 0.932463
 -0.198762 -1.45998 1.72612 1.79473 -0.824075
  0.375903 -1.30752 0.932463 -0.824075 2.65364

julia> F = eigen(A);

julia> Afact = F.vectors * Diagonal(gamma.(F.values .+ 1)) * F.vectors' # "factorial" of A
5×5 Matrix{Float64}:
  1047.56 3786.34 -4796.89 -2069.53 -1358.66
  3786.34 13823.4 -17490.0 -7541.97 -4976.91
 -4796.89 -17490.0 22135.4 9545.66 6294.27
 -2069.53 -7541.97 9545.66 4119.14 2711.5
 -1358.66 -4976.91 6294.27 2711.5 1798.54

```

(More generally, you could use diagonalization for other sorts of matrices, but for non-Hermitian matrices you can run into numerical problems for matrices that are nearly [defective](https://en.wikipedia.org/wiki/Defective_matrix). For non-PSD Hermitian matrices you may also run into poles of the gamma function for negative eigenvalues, but of course those will show up no matter what computational method you use. The gamma function is nicest for PSD matrices.)

Or do you mean the **elementwise** factorial? That’s just `gamma.(A .+ 1)` in Julia, or `factorial.(A)` for integer matrices.

---

<div class="post-metadata">

**Author:** ![ZekeMarshall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekemarshall/32/206876_2.png) [@ZekeMarshall](https://discourse.julialang.org/u/ZekeMarshall)\
**Post date:** [March 12, 2024, 2:04pm UTC](https://discourse.julialang.org/t/matrix-factorials/111515/4 "2024-03-12T14:04:20Z")

</div>

Hi @stevengj , yes that is exactly what I am looking for, a much simpler implementation than I expected and the StackExchange link you sent is a great explanation. Thank you very much!

---

<div class="post-metadata">

**Author:** ![ZekeMarshall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekemarshall/32/206876_2.png) [@ZekeMarshall](https://discourse.julialang.org/u/ZekeMarshall)\
**Post date:** [March 12, 2024, 2:05pm UTC](https://discourse.julialang.org/t/matrix-factorials/111515/5 "2024-03-12T14:05:12Z")

</div>

Hi @Dan , thanks for your reply! The answer by @stevengj below is what I needed.
