# The logarithm of a tensor

**URL:** <https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849>\
**Category:** General Usage\
**Tags:** tensors\
**Created:** [August 13, 2020, 3:30am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849 "2020-08-13T03:30:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [August 13, 2020, 3:30am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/1 "2020-08-13T03:30:33Z")

</div>

I want to take the logarithm of a tensor, what do I do

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 13, 2020, 3:33am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/2 "2020-08-13T03:33:11Z")

</div>

Do you mean element by element? How do you define the operation?  
What does your input look like?

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [August 13, 2020, 3:47am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/3 "2020-08-13T03:47:40Z")

</div>

Let’s say I have A tensor of third order ,then,I want to know how to calculate e^A or log(A)

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [August 13, 2020, 4:06am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/4 "2020-08-13T04:06:41Z")

</div>

![example](https://global.discourse-cdn.com/julialang/original/3X/d/3/d314838a5b3c2e8c7a4cddeded0d88145024ec9f.png)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 13, 2020, 4:14am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/5 "2020-08-13T04:14:59Z")

</div>

I believe this is because tensor multiplication is not defined, unlike a square matrix (where the matrix multiplication is defined)

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 13, 2020, 2:00pm UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/6 "2020-08-13T14:00:41Z")

</div>

Have a look at my tensor package

[https://github.com/chakravala/Grassmann.jl](https://github.com/chakravala/Grassmann.jl)

It supports expnentiation and log of arbitrary tensors, although the fully general log method is not as well implemented as the exp method yet.

It uses a Clifford algebra representation for tensors.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 13, 2020, 2:42pm UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/7 "2020-08-13T14:42:18Z")

</div>

If you want element-wise operations, those are written as `log.(A)` and `exp.(A)` in Julia.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [August 14, 2020, 7:52am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/8 "2020-08-14T07:52:34Z")

</div>

I don’t mean element by element

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [August 14, 2020, 9:28am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/9 "2020-08-14T09:28:54Z")

</div>

I can’t find a logarithm operation in this package

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [August 14, 2020, 9:51am UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/10 "2020-08-14T09:51:09Z")

</div>

It’s not clear to me what you mean by the log of a tensor. Logs are defined when you have an algebra, ie you can square a tensor. I imagine you mean that you contract A\*B with the two last indices of A and the two first of B? If that is the case then you can get your log by reshaping your tensor as a matrix (with `reshape`) and reshaping back.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 14, 2020, 1:25pm UTC](https://discourse.julialang.org/t/the-logarithm-of-a-tensor/44849/11 "2020-08-14T13:25:49Z")

</div>

It’s in there in `src/composite.jl`

[https://github.com/chakravala/Grassmann.jl/blob/master/src/composite.jl](https://github.com/chakravala/Grassmann.jl/blob/master/src/composite.jl)

Tensor multiplication is actually defined, if you use the Clifford product to multiply the tensors representation. The logarithm can be defined in terms of a series expansion with Clifford multiplication. In this case, using `qlog`:

```julia
Base.log(t::T) where T<:TensorAlgebra = qlog((t-1)/(t+1))
Base.log1p(t::T) where T<:TensorAlgebra = qlog(t/(t+2))

```

After first applying a transformation to the input. However, this method can diverge with some input.

In the Grassmann.jl package, there are several options, including an approximate but faster `log_fast` along with the slightly more robust `Base.log` with `qlog`.
