# What is T in : function pca(data::Array{T,2}) where T?

**URL:** <https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811>\
**Category:** General Usage\
**Created:** [August 21, 2019, 6:05pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811 "2019-08-21T18:05:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [August 21, 2019, 6:05pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/1 "2019-08-21T18:05:43Z")

</div>

Is function:

```julia
function pca(data::Array{T,2}) where T
    X = data .- mean(data, dims=2)
    Y = X' ./ sqrt(T(size(X,2)-1))
    U,S,PC = svd(Y)
    S = diagm(0=>S)
    V = S .* S
    
    # find the least variance vector
    indexList = sortperm(diag(V); rev=true)

    PCs = map(x->PC[:,x], indexList)
    return PCs, diag(V)[indexList]
end

```

I have data.  
A need to do all step by step , without function.  
What is ‘T’ in line Y = X’ ./ sqrt(T(size(X,2)-1)) ?

Paul

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 21, 2019, 6:10pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/2 "2019-08-21T18:10:39Z")

</div>

`T` here is the type of data in the array. An `Array` is formally an `Array{T,N} where N where T`. The `T` is the type of data, and the `N` is the number of dimensions.

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [August 21, 2019, 6:14pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/3 "2019-08-21T18:14:28Z")

</div>

Ok: step by step:  
My data

```julia
julia> data=readdlm("v4dane.txt")
2888×4 Array{Float64,2}:
   0.0 0.0 0.0 0.0
   0.0 0.0 0.0 75.0
   0.0 0.0 0.0 100.0
   0.0 0.0 0.0 0.0
....

julia> X = data .- mean(data, dims=2)
2888×4 Array{Float64,2}:
   0.0 0.0 0.0 0.0
 -18.75 -18.75 -18.75 56.25
 -25.0 -25.0 -25.0 75.0
   0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0

julia> Y = X' ./ sqrt(T(size(X,2)-1))
ERROR: UndefVarError: T not defined

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 21, 2019, 6:18pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/4 "2019-08-21T18:18:34Z")

</div>

To expand on Oscar’s answer, the reason for that in this case is to use the same type as your input, so if you give it a matrix of `Float64`s, that will covert what’s returned by `size` to a `Float64`.

In your example, add `T = Float64`.

Separate point - you’re quoting code as a text quote instead of as code. Compare:

> function foo()  
> println(“This doesn’t look like code!”)  
> end

to

```julia
function bar()
    println("Oh, much better!")
end

```

Which look like this when writing:

````julia
> function foo()
> println("This doesn't look like code!")
> end

to

```
function bar()
    println("Oh, much better!")
end
```

````

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 21, 2019, 6:21pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/5 "2019-08-21T18:21:26Z")

</div>

> [@programista](#):
>
> sqrt(Float64(size(X,2)-1)) not works

It should…

```julia
julia> m = rand(5,5);

julia> T = eltype(m)
Float64

julia> sqrt(T(size(m, 2) -1))
2.0

```

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [August 22, 2019, 1:02am UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/6 "2019-08-22T01:02:48Z")

</div>

But wouldn’t it work just as well without the type conversion, e.g. `X ./ sqrt(size(X,2)-1)`?

I would imagine that `size` returns integers, and `sqrt` returns at least floats, such that division `X` by `sqrt` would have type `T` (or higher) anyway. Using `T(size(X))` seems out of place to me, since the type may be promoted by `sqrt` anyway. Or if not out of place, then at least a bit obscure.

I wonder:

1. Is it necessary to cast to a particular type at all? Could one leave out the type, e.g. `X'./sqrt(size(X,2)-1))` with no issues?
2. If one is to cast `T`, would it make more sense after the root, e.g. `T(sqrt(size(X,2)-1)`?
3. Are there more direct or idiomatic (or transparent) ways to ensure type, e.g. `T.(X' ./ sqrt(size(X,2)-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:** [August 22, 2019, 7:10am UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/7 "2019-08-22T07:10:19Z")

</div>

> [@apo383](#):
>
> But wouldn’t it work just as well without the type conversion, e.g. `X ./ sqrt(size(X,2)-1)` ?

I think it would, and you are right, the explicit conversion may not be necessary. The code should be type stable and work fine without that `T`.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 22, 2019, 7:52pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/8 "2019-08-22T19:52:15Z")

</div>

Yeah, I was thinking that myself. Similar formulations are often used to generate Arrays of the correct type (eg `zeros(T, n)`), but in this case `sqrt` seems to always return a Float, regardless of `T`

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 22, 2019, 8:42pm UTC](https://discourse.julialang.org/t/what-is-t-in-function-pca-data-array-t-2-where-t/27811/9 "2019-08-22T20:42:09Z")

</div>

Technically not true. Given complex inputs it will return complex outputs. Not sure about quaternians, my guess is method error.
