Polynomials Example in Julia

Creating a type for the polynomials


In [1]:
workspace()
type poly
    a::AbstractArray{Int64,1}
end

Printing function


In [2]:
function prpoly(p::poly)
sizep = length(p.a)
xstr = "x^"
polystr = ""
(sizep)
    if sizep == 1
      num = p.a[1]
      
      polystr = "$num"
    else
        for i = 1:sizep
        num = p.a[i]

            if num == 0
                    i = i
            elseif i == 1
                polystr ="$num"

            elseif num < 0
                polystr = polystr * " $num $xstr$(i)"
            else
                polystr = polystr * " + $num $xstr$(i-1)"
            end
        end
    end

    if  polystr[2] == '+'
        polystr = polystr[3:end]
    end
return polystr
end

a = poly([1,2,3,4])

println(prpoly(a))


1 + 2 x^1 + 3 x^2 + 4 x^3

Evaluation function


In [3]:
function evpoly(p::poly,r::Real)
    sizep = length(p.a)
    ev = 0
    while sizep > 1
        ev = ev + p.a[sizep]*r
        sizep = sizep - 1
    end
    ev = ev + p.a[1]

end

a = poly([1,2,3,4])
println(evpoly(a,1))


10

Polynomial Addition


In [4]:
function polysum(p::poly, q::poly)
    e = []
    o =length(p.a)
    u =length(q.a)
    if o >= u
        e = p.a
        for i = 1:u
            e[i] = e[i] + q.a[i]

        end
    else
        e = q.a
         for i = 1:o
            e[i] = e[i] + p.a[i]
         end
    end

    return poly(e)

end

a = poly([1,2,3,4])
b = poly([5,0,3])

x = polysum(a,b)
print(prpoly(x))


6 + 2 x^1 + 6 x^2 + 4 x^3

Polynomial Subtraction


In [5]:
function polysub(p::poly, q::poly)
    e = []
    o =length(p.a)
    u =length(q.a)
    if o >= u
        e = p.a
        for i = 1:u
            e[i] = e[i] - q.a[i]

        end
    else
        e = q.a
         for i = 1:o
            e[i] = p.a[i] - e[i]
         end
         y = o+1
         for i = y:u
           e[i] = e[i] * -1
         end
    end

    return poly(e)

end
a = poly([1,2,3,4])
b = poly([5,0,3])

y = polysub(a,b)
println(prpoly(y))


-4 + 2 x^1 + 4 x^3

Polynomial Multiplication


In [6]:
function polymult(p::poly, q::poly)

  e = []
  o =length(p.a)
  u =length(q.a)
  n = 1
    ns = o+u-1
    
    e = fill(0,ns)
    
    ct=1

  for i= 1:o
    for j = 1:u
        e[i+j-1] = e[i+j-1] + p.a[i] * q.a[j]
    end
  end
return poly(e)

end

a = poly([1,2,3,4])
b = poly([5,0,3])

y = polymult(a,b)
println(prpoly(y))


5 + 10 x^1 + 18 x^2 + 26 x^3 + 9 x^4 + 12 x^5

Polynomial Composition


In [7]:
function polycomp(p::poly,q::poly)
    o =length(p.a)
    u =length(q.a)
    e = poly([0])
    i = o
    while i>0 
        r = poly([p.a[i]])
        e = polymult(e,b)
        e = polysum(e,r)
        i= i-1
    end
    return e
  
end

a = poly([1,2,3,4])
b = poly([5,0,3])

y = polycomp(a,b)
println(prpoly(y))


586 + 996 x^2 + 567 x^4 + 108 x^6

Derivative of a Polynomial


In [8]:
function polyder(p::poly)
    o =length(p.a)
    
    if o == 0
        e = poly([0])
        return e
    
    else
        e = fill(0,o-1)
        
        for i = 1:o-1
            e[i]=(i)*p.a[i+1]
        end
    
    end
    return poly(e)
end

a = poly([1,2,3,4])

y = polyder(a)
println(prpoly(y))


2 + 6 x^1 + 12 x^2