# trace(A'\*A)

**URL:** https://discourse.julialang.org/t/trace-a-a/7508
**Category:** General Usage
**Tags:** question
**Created:** [December 4, 2017, 8:14pm UTC](https://discourse.julialang.org/t/trace-a-a/7508 "2017-12-04T20:14:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 4, 2017, 8:14pm UTC](https://discourse.julialang.org/t/trace-a-a/7508/1 "2017-12-04T20:14:28Z")

</div>

Given a (dense) matrix A, what would be a fast, idiomatic way of calculating \text{tr}(A'A)? I can solve it as

```julia
A = Float64.(reshape(1:20, :, 2))
trace(A*A') # = 2870, but obviously wasteful
sum(i -> (a = @view A[i, :]; dot(a, a)), 1:size(A, 1)) # works, convoluted

```

I imagine there must be a simple solution that is compiled to some neat BLAS function, but I can’t come up with anything.

---

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [December 4, 2017, 8:17pm UTC](https://discourse.julialang.org/t/trace-a-a/7508/2 "2017-12-04T20:17:54Z")

</div>

It is Monday so I could be horribly wrong here but isn’t tr(A'A) the sum of the squares of the elements of A? If I am correct then you just need

```julia
sum(abs2, A)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 4, 2017, 8:26pm UTC](https://discourse.julialang.org/t/trace-a-a/7508/3 "2017-12-04T20:26:41Z")

</div>

Thanks. It is pretty clear that I should stop programming for today 😄

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [December 5, 2017, 5:54am UTC](https://discourse.julialang.org/t/trace-a-a/7508/4 "2017-12-05T05:54:44Z")

</div>

> [@dmbates](#):
>
> It is Monday so I could be horribly wrong here but isn’t tr(A’A) the sum of the squares of the elements of A? If I am correct then you just need
> 
> sum(abs2, A)

~~Just a note for clarity in case anyone else is reading this, the trace is the sum of the _diagonal_ elements of A, not all of the elements.~~

~~Interestingly, for a reason that is not obvious to me, your solution does in fact give the correct trace though, whereas if we defined some square matrix~~

```julia
B = A*A'

```

~~then~~

```julia
trace(B)

```

~~is **not** equal to~~

```julia
sum(identity,B)

```

~~so I don’t see why~~

```julia
sum(abs2,A)

```

~~does give the right answer.~~

**Edit:**  
I should have known better than to comment late at night on linear algebra. I completely forgot the product identities

---

<div class="post-metadata">

### Author: ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)
#### Post date: [December 5, 2017, 6:33am UTC](https://discourse.julialang.org/t/trace-a-a/7508/5 "2017-12-05T06:33:06Z")

</div>

Check sources such as [Wikipedia](https://en.wikipedia.org/wiki/Trace_(linear_algebra)) to confirm that the trace of a product `X*Y.'` can be written as `sum(X .* Y)`. The difference is, you are using `sum(identity, B)` where `B = X*Y.'` already. Then, you need to use `sum(identity, diag(B))`. In the solution provided, he is using `A` not `A*A.'`, avoiding matrix multiplication and vectorizing the operations.

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [December 5, 2017, 6:34am UTC](https://discourse.julialang.org/t/trace-a-a/7508/6 "2017-12-05T06:34:22Z")

</div>

> [@Mason](#):
>
> trace is the sum of the squares of the diagonal elements of A

The trace of a matrix is simply the sum of the diagonal elements. The squaring is a result of the multiplication A’A.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [December 5, 2017, 6:55am UTC](https://discourse.julialang.org/t/trace-a-a/7508/7 "2017-12-05T06:55:48Z")

</div>

> [@aytekinar](#):
>
> Check sources such as Wikipedia to confirm that the trace of a product X_Y.’ can be written as sum(X ._ Y). The difference is, you are using sum(identity, B) where B = X_Y.’ already. Then, you need to use sum(identity, diag(B)). In the solution provided, he is using A not A_A.', avoiding matrix multiplication and vectorizing the operations.

> [@Per](#):
>
> trace is the sum of the squares of the diagonal elements of A
> 
> The trace of a matrix is simply the sum of the diagonal elements. The squaring is a result of the multiplication A’A.

Yikes. Now its me who needs to stop programming and go to bed. Forgot about the product identities **and** mistyped the definition of trace.
