# Vector of monomials of order up to n?

**URL:** <https://discourse.julialang.org/t/vector-of-monomials-of-order-up-to-n/30308>\
**Category:** General Usage\
**Created:** [October 25, 2019, 3:02pm UTC](https://discourse.julialang.org/t/vector-of-monomials-of-order-up-to-n/30308 "2019-10-25T15:02:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 25, 2019, 3:02pm UTC](https://discourse.julialang.org/t/vector-of-monomials-of-order-up-to-n/30308/1 "2019-10-25T15:02:19Z")

</div>

Suppose I have a scalar x. Then producing the vector [1,x,x^2, \ldots,x^n] is straightforward,

```julia
[x^i for i in 0:n]

```

What if x is a vector, x=[x\_1,\dots,x\_{n\_x}], and I want to produce the vector [1, x\_1, \ldots,x\_{n\_x}, x\_1^2, x\_1 x\_2, \ldots, x\_1 x\_{n\_x}, x\_2^2, x\_2 x\_3, \ldots, x\_1^n, x\_1^{n-1}x\_2, \ldots]. How can I do that in the simplest and most elegant/efficient way? Is there a package for this?

---

<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:** [October 25, 2019, 3:31pm UTC](https://discourse.julialang.org/t/vector-of-monomials-of-order-up-to-n/30308/2 "2019-10-25T15:31:47Z")

</div>

Use the [Combinatorics](https://github.com/JuliaMath/Combinatorics.jl) package:

```julia
using Combinatorics

prods(x, n) = collect(Iterators.flatten((prod(y) for y in with_replacement_combinations(x, i)) for i = 0:n))

```

For example, using the SymPy package to apply this to symbolic variables, I get:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/aeeda3513e88dd107ba608598b3a842fb71f29f4.png)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 25, 2019, 5:08pm UTC](https://discourse.julialang.org/t/vector-of-monomials-of-order-up-to-n/30308/3 "2019-10-25T17:08:34Z")

</div>

Thanks! I’ll test it to see if I understand it :-).

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 25, 2019, 5:41pm UTC](https://discourse.julialang.org/t/vector-of-monomials-of-order-up-to-n/30308/4 "2019-10-25T17:41:41Z")

</div>

It works perfectly as I wanted it to! Thanks!
