# How to Append Polynomial to 5-element vector if the highest order is less than 4? Polynomials.jl Package

**URL:** https://discourse.julialang.org/t/how-to-append-polynomial-to-5-element-vector-if-the-highest-order-is-less-than-4-polynomials-jl-package/90904
**Category:** General Usage
**Tags:** question, package
**Created:** [November 27, 2022, 3:20pm UTC](https://discourse.julialang.org/t/how-to-append-polynomial-to-5-element-vector-if-the-highest-order-is-less-than-4-polynomials-jl-package/90904 "2022-11-27T15:20:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [November 27, 2022, 3:20pm UTC](https://discourse.julialang.org/t/how-to-append-polynomial-to-5-element-vector-if-the-highest-order-is-less-than-4-polynomials-jl-package/90904/1 "2022-11-27T15:20:35Z")

</div>

Hi all I have a problem here,

I want to calculate special sum by using `Polynomials.jl`, thus it works fine if there is one of degree 4, but if the highest input is of degree 2, the `coeffs(p)` would become 3-element vector, and we need to append the last two elements by 0 so we can still make the calculation of

`a*(coeffs(p))`

this is the code:

```julia
using Symbolics, Polynomials
@variables n i 

a = [1 n*(n+1)/2 n*(n+1)*(2n+1)/6 (1/4)*(n*(n+1))^2 (n*(n+1)*(2n+1)*(3n^2 + 3n - 1))/30]

# 1 + 2i -> Polynomial([1,2,0,0], :i)
# 3i^2 + i^4 -> Polynomial([0,0,3,0,1], :i)
# i^2 - 3i - 10 -> Polynomial([-10,-3,1,0,0], :i)

p = Polynomial([-10,-3,1,0,1], :i)

# coeffs(p) -> returns the entire coefficient vector
# Polynomials.degree(p) -> returns the polynomial degree, length is 1 plus the degree
println("The special sum for $p is")

a*(coeffs(p))

```

Anyone know how to append polynomial to 5-element vector with zeroes ? if the input polynomial is less than order of 4?

Thus, if I input this:

`p = Polynomial([-10,-3,1,0,0], :i)`

it would become still 5-element vector: `[-10; -3; -1; 0; 0]` instead of 3-element vector `[-10;-3;1]`

either use `if-else` conditional or any idea?

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 27, 2022, 6:11pm UTC](https://discourse.julialang.org/t/how-to-append-polynomial-to-5-element-vector-if-the-highest-order-is-less-than-4-polynomials-jl-package/90904/2 "2022-11-27T18:11:12Z")

</div>

Does this `pad` function solve the problem?

```julia
julia> pad(v,n) = (L = length(v) ; 
  [i <= L ? v[i] : zero(eltype(v)) for i in 1:n] )
pad (generic function with 1 method)

julia> pad(coeffs(p),5)
5-element Vector{Int64}:
 -10
  -3
   1
   0
   0

```

`pad(v,n)` will return vector of length `n` with elements from `v` where valid and zero otherwise.  
A similar function might exist in some library, or might be added as it sounds useful.

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [November 27, 2022, 6:30pm UTC](https://discourse.julialang.org/t/how-to-append-polynomial-to-5-element-vector-if-the-highest-order-is-less-than-4-polynomials-jl-package/90904/3 "2022-11-27T18:30:11Z")

</div>

Wow @Dan Amazing!

How did you know about this `pad(v,n)`, this is what I need. Thanks a lot! All the best for you
