# Alternative way to create following array raised to powers?

**URL:** <https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154>\
**Category:** New to Julia\
**Tags:** matrices\
**Created:** [January 6, 2022, 5:32pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154 "2022-01-06T17:32:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [January 6, 2022, 5:32pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/1 "2022-01-06T17:32:07Z")

</div>

How to do following more efficiently (any alternative way ?)

```julia
julia> a = [-1,0,1] # my array
3-element Vector{Int64}:
 -1
  0
  1

julia> hcat([a.^i for i in 0:3]...)' # array raised to power 0:n and then transposed
4×3 adjoint(::Matrix{Int64}) with eltype Int64:
  1 1 1
 -1 0 1
  1 0 1
 -1 0 1

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 6, 2022, 5:43pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/2 "2022-01-06T17:43:13Z")

</div>

Try this:

```julia
reduce(hcat, [a.^i for i in 0:3])' 

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 6, 2022, 5:43pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/3 "2022-01-06T17:43:27Z")

</div>

You can use broadcasting with one dimension for each input array:

```julia
julia> [-1 0 1] .^ (0:3)
4×3 Matrix{Int64}:
  1 1 1
 -1 0 1
  1 0 1
 -1 0 1

```

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [January 6, 2022, 5:46pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/4 "2022-01-06T17:46:40Z")

</div>

Wow… How I missed this I don’t understand… I was trying same but was writing array as `[-1, 0,1]`  
It was giving error 😅

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 6, 2022, 5:53pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/5 "2022-01-06T17:53:08Z")

</div>

> [@yewalenikhil65](#):
>
> I was trying same but was writing array as `[-1, 0,1]`

`[-1, 0, 1]' .^ (0:3)`

But frankly, I’m quite surprised at the way it works, i.e. returning a matrix instead of a vector of vectors, for example…

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 6, 2022, 6:00pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/6 "2022-01-06T18:00:59Z")

</div>

How so? This is how broadcasting always works. `f.(row, column)` always returns a matrix. And not just in Julia. Broadcasting in Python and Matlab does the same except for the dot notation.

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [January 6, 2022, 6:06pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/7 "2022-01-06T18:06:40Z")

</div>

Possibly because `[1 0 1]` is a 1x3 Matrix , and not a vector… So broadcasting a matrix type is giving us a Matrix itself?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 6, 2022, 6:16pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/8 "2022-01-06T18:16:29Z")

</div>

@DNF, thank you for framing this properly as I totally missed the point, but can now see this is a common pattern.

---

<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:** [January 6, 2022, 8:05pm UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/9 "2022-01-06T20:05:16Z")

</div>

> [@yewalenikhil65](#):
>
> How to do following more efficiently (any alternative way ?)

What you are constructing is a [Vandermonde matrix](https://en.wikipedia.org/wiki/Vandermonde_matrix) (transposed). A much faster way to construct it is to use two nested loops, as described in [another discourse thread](https://discourse.julialang.org/t/efficient-creation-of-power-series-matrix-or-array-of-arrays/18988/7).

The two-loop approach is about 30x faster on my machine for constructing a 100x100 Vandermonde matrix, compared to the broadcasting approach suggested above:

```julia
julia> using BenchmarkTools

julia> vander(x, n=length(x)) = .... # two-loop version from discourse

julia> bvander(x, n=length(x)) = x .^ (0:n-1)'; # broadcast-based vandermonde

julia> x = rand(100);

julia> @btime vander($x);
  5.892 μs (2 allocations: 78.17 KiB)

julia> @btime bvander($x);
  193.366 μs (2 allocations: 78.17 KiB)

```

The reason the two-loop version can be so much faster is that it doesn’t compute each power separately, but instead accumulates the powers one multiplication at a time from the previous powers.

(The broadcasting version is much more compact, though, so if it’s not too performance-critical I would stick with that!)

Another option is to create the matrix lazily, e.g. [SpecialMatrices](https://github.com/JuliaMatrices/SpecialMatrices.jl) has methods to implicitly create such matrices and work with them. The main advantage here is that if you are doing things like solving linear systems, there are specialized algorithms for Vandermonde matrices.

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [January 7, 2022, 4:15am UTC](https://discourse.julialang.org/t/alternative-way-to-create-following-array-raised-to-powers/74154/10 "2022-01-07T04:15:40Z")

</div>

@stevengj @sijo  
Both of your solutions are useful as well as good learning experience in julia. I wish discourse had ability to mark both solutions as `Solution` 😅
