diff --git a/tareas/Carlos/AutomTaylor.jl b/tareas/Carlos/AutomTaylor.jl new file mode 100644 index 0000000..cf6baf1 --- /dev/null +++ b/tareas/Carlos/AutomTaylor.jl @@ -0,0 +1,209 @@ +__precompile__(true) +module BD + import Base: +, -, *, /, == #importo las operaciones principales + export Taylor + + #incluyendo, primero que nada, la definición de Taylor. + +type Taylor{T<:Number} + orden::Int #el orden de la serie de taylor. + coff::Array{T,1} #coeficientes en la serie de taylor. + function Taylor(orden::Int,coff::Array{T,1})#ESTE AGREGADO ES NECESARIO PARA GARANTIZAR QUE TODOS LOS ELEMENTOS INGRESADOS EN EL VECTOR SEAN DEL MISMO ORDEN QUE LA N PROPUESTA + if length(coff)==orden+1 + new(orden,coff) + else + error("función no válida ya que el orden no coincide con el número de coeficientes") + end + end +end +#se especifican los posibles casos para los que se puede usar este taylor. +Taylor{T<:Number}(orden::Int,coff::Array{T,1})=Taylor{T}(orden,coff) #el caso general +Taylor{T<:Number}(coff::Array{T,1})=Taylor{T}(length(coff-1),coff) +Taylor{T<:Number}(orden::Int,coff::T)=Taylor{T}(orden,[coff]) #este caso particular es para escalares + +#esta parte es para ver si los elementos son del mismo orden para hacer la suma y resta, de no ser así, obtener el orden deseado para hacer la suma +function igualavector!(a,b) + if length(a.coff) < length(b.coff) + for i in length(a.coff):length(b.coff)-1 + push!(a.coff,0) + end + return a.coff + elseif length(a.coff) > length(b.coff) + for i in length(b.coff):length(a.coff)-1 + push!(b.coff,0) + end + return b.coff + end +end + +#suma de taylor +function +(f::Taylor,g::Taylor) + igualavector!(f,g) #igualdor de taylor's + return Taylor(f.orden,f.coff+g.coff) #operación artimética directa, la resta es análoga +end + +#resta de taylor +function -(f::Taylor,g::Taylor) + igualavector!(f,g) #se usa el igualador de taylor's + return Taylor(f.orden,f.coff-g.coff) +end + +#producto entre taylors +#funcion auxiliar para hacer el producto +function prod!(f::Taylor,g::Taylor) + m=Taylor(f.orden+g.orden,zeros(f.orden+g.orden+1)) #taylor auxiliar para poder hacer el producto + f=igualavector!(f,m) + g=igualavector!(g,m) #con esto se tienen vectores de mismo orden y coeficientes + return f,g +end +#fin de l funcion axiliar +#funcion principal de taylor +function *(f::Taylor,g::Taylor) #con la funcion prod ya formulada, se puede hacer producto ya que son del mismo orden + prod!(f,g) #se usa el promotor de coeficientes + z=Taylor(f.orden,zeros(f.orden+1)) + for i in 1:length(f.coff) #se usa doble ciclo for para poder hacer el producto por cada coeficiente y depositarl en el resultado + for j in 1:i + z.coff[i]+=f.coff[j]*g.coff[i+1-j] + end + end + return z +end +*(A::Real,B::Taylor)=Taylor(B.orden,A*B.coff) +*(A::Taylor,B::Real)=Taylor(A.orden,B*A.coff) +#fin del producto + +#comparación +==(A::Taylor,B::Taylor)=(A.orden==B.orden && A.coff==B.coff) + +#cociente entre taylors +function /(f::Taylor,g::Taylor) + d=f.orden-g.orden #el discriminante para ver si la división da como resultado un polinomio + if d<0 + error("división no válida ya que el resultado no es un polinomio de grado mayor a cero") + end + #fin del if + igualavector!(f,g) #se igualan los coeficientes para poder operar en la división + cociente=zeros(f.orden+1) + h=1 #contador que indica donde se empieza a ser el resultado distinto de cero + for i in 1:length(g.coff) + if g.coff[i]!=0 + cociente[1]=(f.coff[i])/(g.coff[i]) #es es para hacer la división y evitar dividir coeficientes cero + h=i + break + elseif (i==length(g.coff)) & (g.coff[i]==0) #en caso de encontrar un coeficiente del denominador con cero, manda un error + error("polinomio divisor posee un cero") + end + end + #fin del for + for i in 1:h-1 #usando el contador del ciclo pasado + if f.coff[i]!=0 + error("no tiene una expansión de Taylor") #esto es para asegurar que no sea de la forma a^n/b^m donde m>n + end + end + #lo que falta ahora, es hacer el resto de la división si el polinomio cumple con lo estipulado previamente + #como en el producto, en el cociente se hace doble ciclo for + for i in h+1:length(cociente)-h + suma=0 #suma de los resultados que se lleven a cabo en la division + for j in 1:i-h + suma=suma+(cociente[j])*g.coff[i-j+1] + end + cociente[1+i-h]=(1/g.coff[h])*(f.coff[i]-suma) + end + if cociente[d+2]==0 + deleteat!(cociente,d+2) + end + return Taylor(d,cociente) +end +/(A::Taylor,B::Real)=Taylor(A.orden,(A.coff)/B) +#fin de la división + +import Base:exp,log,^,sin,cos #importando mas funciones +#anexo para funciones del problema 2 + +#exponencial +function exp(f::Taylor) #se implementa la función de la exponencial + v=zeros(Number,f.orden+1) + v[1]=exp(f.coff[1]) + for i in 2:length(v) + for j in 1:i-1 + v[i]+=(i-j)*(f.coff[i-j+1])*(v[j]) + end + v[i]=(v[i])/(i-1) + end + return Taylor(length(v)-1,v) +end +#fin de la exponencial + +#logaritmo +function log(f::Taylor) #se implementa la función de logaritmo + L=zeros(length(f.coff)) + m=0 + L[1]=log(f.coff[1]) + L[2]=(f.coff[2])/(f.coff[1]) + for i in 2:length(f.coff)-1 + for j in 1:i-1 + m+=j*L[j+1]*(f.coff[i+1-j]) + end + L[i+1]=(1/f.coff[1])*(f.coff[i+1]-m/i) + end + return Taylor(length(L)-1,L) +end +#fin del logaritmo + +#potencias +function ^(f::Taylor,a::Int) #funcion que implementa la exponencial + P=zeros(length(f.coff)) + m=0 + if a==0 + Taylor(length(f.coff),zeros(length(f.coff))) + else + P[1]=(f.coff[1])^(a) + P[2]=a*((f.coff[2])/(f.coff[1]))*P[1] + for i in 1:length(f.coff)-1 + for j in 1:i-1 + m+=((a*i)-(a+1)*(j))*(f.coff[i+1-j])*(P[j+1]) + end + P[i+1]=(a)*(f.coff[i+1]/f.coff[1])*P[1]+(1/(i*f.coff[1]))*m + end + return Taylor(length(P)-1,P) + end +end +#fin de potencias + +#seno +function sin(f::Taylor) #creando la función de seno + S=zeros(length(f.coff)) + C=zeros(length(f.coff)) + C[1]=cos(f.coff[1]) + S[2]=C[1] #se hará lo mismo para coseno + m=0 + for i in 2:length(f.coff)-1 + for j in 0:i-1 + m+=(i-j)*(f.coff[i+1-j])*(S[j+1]) + end + S[i+1]=m/i + end + return Taylor(length(S)-1,S) +end +#fin de seno + +#coseno +function cos(f::Taylor) #creando la funcion coseno, semejante a la del seno + S=zeros(length(f.coff)) + C=zeros(length(f.coff)) + C[1]=cos(f.coff[1]) + S[2]=C[1] + m=0 + for i in 2:length(f.coff)-1 + for j in 0:i-1 + m+=(i-j)*f(f.coff[i+1-j])*(C[j+1]) + end + C[i+1]=-(m/i) + end + return Taylor(length(C)-1,C) +end +#fin de coseno +#fin del agregado + + +end diff --git a/tareas/Carlos/Tarea5.ipynb b/tareas/Carlos/Tarea5.ipynb new file mode 100644 index 0000000..77602ea --- /dev/null +++ b/tareas/Carlos/Tarea5.ipynb @@ -0,0 +1,1132 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tarea5: Series de Taylor" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Fecha de envío del PR inicial: **viernes 21 de abril**\n", + "\n", + "Fecha de aceptación del PR: **martes 25 de abril, antes de la clase**" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ejercicio 1\n", + "\n", + "Implementen una nueva estructura paramétrica (`type`) que defina el tipo `Taylor`, donde el parámetro debe ser un subtipo de `Number`. Definan métodos que implementen las operaciones aritméticas básicas (`+`, `-`, `*`, `/`) y la igualdad (`==`). Esto deberá ser incluido en un módulo.\n", + "\n", + "Incluyan pruebas (en el archivo \"runtests_taylor.jl\") para cada uno de los métodos que implementen.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{T<:Number}" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + "Definición de polinomios de Taylor, donde esta compuesto por un numero que denota el orden (es de tipo entero)\n", + "dado por un valor n y un vector de un orden n+1\n", + "\"\"\"\n", + "type Taylor{T<:Number}\n", + " orden::Int #el orden de la serie de taylor.\n", + " coff::Array{T,1} #coeficientes en la serie de taylor.\n", + " function Taylor(orden::Int,coff::Array{T,1})#ESTE AGREGADO ES NECESARIO PARA GARANTIZAR QUE TODOS LOS ELEMENTOS INGRESADOS EN EL VECTOR SEAN DEL MISMO ORDEN QUE LA N PROPUESTA\n", + " if length(coff)==orden+1\n", + " new(orden,coff) \n", + " else\n", + " error(\"función no válida ya que el orden no coincide con el número de coeficientes\")\n", + " end\n", + " end\n", + "end\n", + "#se especifican los posibles casos para los que se puede usar este taylor.\n", + "Taylor{T<:Number}(orden::Int,coff::Array{T,1})=Taylor{T}(orden,coff) #el caso general\n", + "Taylor{T<:Number}(coff::Array{T,1})=Taylor{T}(length(coff-1),coff)\n", + "Taylor{T<:Number}(orden::Int,coff::T)=Taylor{T}(orden,[coff]) #este caso particular es para escalares " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3-element Array{Int64,1}:\n", + " 1\n", + " 2\n", + " 3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a=[1,2,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "LoadError", + "evalue": "función no válida ya que el orden no coincide con el número de coeficientes", + "output_type": "error", + "traceback": [ + "función no válida ya que el orden no coincide con el número de coeficientes", + "", + " in Type at .\\In[1]:12 [inlined]", + " in Taylor{T<:Number}(::Int64, ::Array{Int64,1}) at .\\In[1]:17" + ] + } + ], + "source": [ + "Taylor(1,a)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(2,[1,2,3])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Taylor(2,a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Esto sólo es un ejemplo para ver que el nuevo type *Taylor* funciona correctamente" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(0,[1])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Taylor(0,1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Este Taylor nos da el caso de una función del tipo constante, ya que éstas son de orden cero" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "igualavector! (generic function with 1 method)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function igualavector!(a,b) #esta funcion compara los valores de los órdenes de los polinomios\n", + " if length(a.coff) < length(b.coff) \n", + " for i in length(a.coff):length(b.coff)-1 \n", + " push!(a.coff,0)\n", + " end\n", + " a.orden=b.orden\n", + " return a,b #sea cualquier caso, de no ser del mismo orden, el de orden menor aumenta su grado para ser igual al de grado mayor\n", + " elseif length(a.coff) >length(b.coff)\n", + " for i in length(b.coff):length(a.coff)-1\n", + " push!(b.coff,0)\n", + " end \n", + " b.orden=a.orden\n", + " return a,b\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(1,[1,2])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j=Taylor(0,1)\n", + "k=Taylor(1,[1,2])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(Taylor{Int64}(1,[1,0]),Taylor{Int64}(1,[1,2]))" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "igualavector!(j,k)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import Base:+,-,*,/,== ##importando las operaciones principales de taylor:\n", + "#suma,resta,multiplicacion, división y comparación" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "+ (generic function with 164 methods)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function +(f::Taylor,g::Taylor) \n", + " igualavector!(f,g) #igualdor de taylor's\n", + " return Taylor(f.orden,f.coff+g.coff) #operación artimética directa, la resta es análoga\n", + "end\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(1,[2,2])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j+k" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "La suma funciona correctamente, se procede a hacer la resta." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "- (generic function with 191 methods)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function -(f::Taylor,g::Taylor)\n", + " igualavector!(f,g) #se usa el igualador de taylor's\n", + " return Taylor(f.orden,f.coff-g.coff) \n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(1,[0,-2])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j-k" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(1,[1,1])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j=Taylor(1,[1,1])\n", + "k=Taylor(1,[1,1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Una vez definido la suma y la resta, falta programar la multiplicación, división y comparación" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "prod! (generic function with 1 method)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function prod!(f::Taylor,g::Taylor)\n", + " m=Taylor(f.orden+g.orden,zeros(f.orden+g.orden+1)) #taylor auxiliar para poder hacer el producto\n", + " f=igualavector!(f,m)\n", + " g=igualavector!(g,m) #con esto se tienen vectores de mismo orden y coeficientes\n", + " return f,g\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "* (generic function with 152 methods)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function *(f::Taylor,g::Taylor) #con la funcion prod ya formulada, se puede hacer producto ya que son del mismo orden\n", + " prod!(f,g) #se usa el promotor de coeficientes\n", + " z=Taylor(f.orden,zeros(f.orden+1)) \n", + " for i in 1:length(f.coff) #se usa doble ciclo for para poder hacer el producto por cada coeficiente y depositarl en el resultado\n", + " for j in 1:i\n", + " z.coff[i]+=f.coff[j]*g.coff[i+1-j]\n", + " end\n", + " end\n", + " return z\n", + "end\n", + "*(A::Real,B::Taylor)=Taylor(B.orden,A*B.coff)\n", + "*(A::Taylor,B::Real)=Taylor(A.orden,B*A.coff)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(2,[1.0,2.0,1.0])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j*k" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(2,[1.0,2.0,1.0])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "w=ans" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(2,[2.0,4.0,2.0])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2*w" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(2,[2.0,4.0,2.0])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "w*2" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Base.:==" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + "la comparacion de taylors\n", + "\"\"\"\n", + "==(A::Taylor,B::Taylor)=(A.orden==B.orden && A.coff==B.coff)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "true" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Taylor(1,[1,1])==Taylor(1,[1,1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Ya definido el producto, solo falta la división" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "/ (generic function with 55 methods)" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function /(f::Taylor,g::Taylor)\n", + " d=f.orden-g.orden #el discriminante para ver si la división da como resultado un polinomio\n", + " if d<0\n", + " error(\"división no válida ya que el resultado no es un polinomio de grado mayor a cero\")\n", + " end\n", + " #fin del if\n", + " igualavector!(f,g) #se igualan los coeficientes para poder operar en la división\n", + " cociente=zeros(f.orden+1)\n", + " h=1 #contador que indica donde se empieza a ser el resultado distinto de cero\n", + " for i in 1:length(g.coff)\n", + " if g.coff[i]!=0\n", + " cociente[1]=(f.coff[i])/(g.coff[i]) #es es para hacer la división y evitar dividir coeficientes cero\n", + " h=i\n", + " break\n", + " elseif (i==length(g.coff)) & (g.coff[i]==0) #en caso de encontrar un coeficiente del denominador con cero, manda un error\n", + " error(\"polinomio divisor posee un cero\")\n", + " end\n", + " end\n", + " #fin del for\n", + " for i in 1:h-1 #usando el contador del ciclo pasado\n", + " if f.coff[i]!=0\n", + " error(\"no tiene una expansión de Taylor\") #esto es para asegurar que no sea de la forma a^n/b^m donde m>n\n", + " end\n", + " end\n", + " #lo que falta ahora, es hacer el resto de la división si el polinomio cumple con lo estipulado previamente\n", + " #como en el producto, en el cociente se hace doble ciclo for\n", + " for i in h+1:length(cociente)-h\n", + " suma=0 #suma de los resultados que se lleven a cabo en la division\n", + " for j in 1:i-h\n", + " suma=suma+(cociente[j])*g.coff[i-j+1]\n", + " end\n", + " cociente[1+i-h]=(1/g.coff[h])*(f.coff[i]-suma)\n", + " end\n", + " for i in d+2:length(f.coff)\n", + " if cociente[i]==0\n", + " deleteat!(cociente,i)\n", + " end\n", + " end\n", + " return Taylor(d,cociente)\n", + "end\n", + "/(A::Taylor,B::Real)=Taylor(A.orden,(A.coff)/B)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(3,[1,3,3,1])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "j=Taylor(1,[1,1])\n", + "k=Taylor(3,[1,3,3,1])" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(2,[1.0,2.0,1.0])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "k/j" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Int64}(4,[2,4,6,8,0])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Taylor(3,[1,2,3,4])+Taylor(4,[1,2,3,4,0])" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1m\u001b[37mTest Summary: | \u001b[0m\u001b[1m\u001b[32mPass \u001b[0m\u001b[1m\u001b[34mTotal\u001b[0m\n", + " pruebas realizadas | \u001b[1m\u001b[32m 14 \u001b[0m\u001b[1m\u001b[34m 14\u001b[0m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING: replacing module BD\n", + "WARNING: using BD.Taylor in module Main conflicts with an existing identifier.\n" + ] + }, + { + "data": { + "text/plain": [ + "Base.Test.DefaultTestSet(\"pruebas realizadas\",Any[\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(0,[1]) + Taylor(1,[1,2]) == Taylor(1,[2,2])\n", + " Evaluated: Taylor{Int64}(1,[2,2]) == Taylor{Int64}(1,[2,2]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(1,[1,1]) + Taylor(1,[1,1]) == Taylor(1,[2,2])\n", + " Evaluated: Taylor{Int64}(1,[2,2]) == Taylor{Int64}(1,[2,2]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(0,[1]) - Taylor(1,[1,2]) == Taylor(1,[0,-2])\n", + " Evaluated: Taylor{Int64}(1,[0,-2]) == Taylor{Int64}(1,[0,-2]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(1,[1,1]) - Taylor(1,[1,1]) == Taylor(1,[0,0])\n", + " Evaluated: Taylor{Int64}(1,[0,0]) == Taylor{Int64}(1,[0,0]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: 3 * Taylor(2,[10,20,30]) == Taylor(2,[30,60,90])\n", + " Evaluated: Taylor{Int64}(2,[30,60,90]) == Taylor{Int64}(2,[30,60,90]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(1,[1,1]) * Taylor(1,[1,1]) == Taylor(2,[1,2,1])\n", + " Evaluated: Taylor{Float64}(2,[1.0,2.0,1.0]) == Taylor{Int64}(2,[1,2,1]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(3,[2,4,6,8]) / 2 == Taylor(3,[1,2,3,4])\n", + " Evaluated: Taylor{Float64}(3,[1.0,2.0,3.0,4.0]) == Taylor{Int64}(3,[1,2,3,4]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(2,[1,2,1]) / Taylor(1,[1,1]) == Taylor(1,[1,1])\n", + " Evaluated: Taylor{Float64}(1,[1.0,1.0]) == Taylor{Int64}(1,[1,1]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(1,[1,2]) == Taylor(1,[1,2])\n", + " Evaluated: Taylor{Int64}(1,[1,2]) == Taylor{Int64}(1,[1,2]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: exp(Taylor(0,[0])) == Taylor(0,[1.0])\n", + " Evaluated: Taylor{Number}(0,Number[1.0]) == Taylor{Float64}(0,[1.0]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: Taylor(1,[2,0]) ^ 2 == Taylor(1,[4,0])\n", + " Evaluated: Taylor{Float64}(1,[4.0,0.0]) == Taylor{Int64}(1,[4,0]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: sin(Taylor(1,[0,0])) == Taylor(1,[0,1])\n", + " Evaluated: Taylor{Float64}(1,[0.0,1.0]) == Taylor{Int64}(1,[0,1]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: cos(Taylor(1,[0,0])) == Taylor(1,[1,0])\n", + " Evaluated: Taylor{Float64}(1,[1.0,0.0]) == Taylor{Int64}(1,[1,0]),\u001b[1m\u001b[32mTest Passed\n", + "\u001b[0m Expression: log(Taylor(1,[1,1])) == Taylor(1,[0,1])\n", + " Evaluated: Taylor{Float64}(1,[0.0,1.0]) == Taylor{Int64}(1,[0,1])],false)" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Muestren que su código funciona con tests adecuados\n", + "using Base.Test\n", + "include(\"runtest_taylor.jl\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ejercicio 2\n", + "\n", + "Obtengan las relaciones de recurrencia para las funciones $L(x) = \\log\\big(g(x)\\big)$, $P_\\alpha(x) = \\big(g(x)\\big)^\\alpha$, $S(x) = \\sin\\big(g(x)\\big)$, $C(x) = \\cos\\big(g(x)\\big)$ usando el mismo procedimiento que arriba. Implementen métodos adecuados para estas funciones en el módulo, actuando sobre estructuras `Taylor` e incluyan pruebas necesarias en `runtest_taylor.jl`." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "exp (generic function with 13 methods)" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import Base:exp,log,^,sin,cos #importando mas funciones\n", + "function exp(f::Taylor) #se implementa la función de la exponencial\n", + " v=zeros(Number,f.orden+1)\n", + " v[1]=exp(f.coff[1])\n", + " for i in 2:length(v)\n", + " for j in 1:i-1\n", + " v[i]+=(i-j)*(f.coff[i-j+1])*(v[j])\n", + " end\n", + " v[i]=(v[i])/(i-1)\n", + " end\n", + " return Taylor(length(v)-1,v)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Number}(0,Number[1.0])" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "exp(Taylor(0,[0]))" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "log (generic function with 20 methods)" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function log(f::Taylor) #se implementa la función de logaritmo\n", + " L=zeros(length(f.coff))\n", + " m=0\n", + " L[1]=log(f.coff[1])\n", + " L[2]=(f.coff[2])/(f.coff[1])\n", + " for i in 2:length(f.coff)-1\n", + " for j in 1:i-1\n", + " m+=j*L[j+1]*(f.coff[i+1-j])\n", + " end\n", + " L[i+1]=(1/f.coff[1])*(f.coff[i+1]-m/i)\n", + " end\n", + " return Taylor(length(L)-1,L)\n", + "end\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(1,[0.0,1.0])" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "log(Taylor(1,[1,1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "^ (generic function with 49 methods)" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function ^(f::Taylor,a::Int) #funcion que implementa la exponencial\n", + " P=zeros(length(f.coff))\n", + " m=0\n", + " if a==0\n", + " Taylor(length(f.coff),zeros(length(f.coff)))\n", + " else\n", + " P[1]=(f.coff[1])^(a)\n", + " P[2]=a*((f.coff[2])/(f.coff[1]))*P[1]\n", + " for i in 1:length(f.coff)-1\n", + " for j in 1:i-1\n", + " m+=((a*i)-(a+1)*(j))*(f.coff[i+1-j])*(P[j+1])\n", + " end\n", + " P[i+1]=(a)*(f.coff[i+1]/f.coff[1])*P[1]+(1/(i*f.coff[1]))*m\n", + " end\n", + " return Taylor(length(P)-1,P)\n", + " end\n", + "end " + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(1,[4.0,0.0])" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Taylor(1,[2,0])^2" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "sin (generic function with 12 methods)" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function sin(f::Taylor) #creando la función de seno\n", + " S=zeros(length(f.coff))\n", + " C=zeros(length(f.coff))\n", + " C[1]=cos(f.coff[1])\n", + " S[2]=C[1] #se hará lo mismo para coseno\n", + " m=0\n", + " for i in 2:length(f.coff)-1\n", + " for j in 0:i-1\n", + " m+=(i-j)*(f.coff[i+1-j])*(S[j+1])\n", + " end\n", + " S[i+1]=m/i\n", + " end\n", + " return Taylor(length(S)-1,S)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(1,[0.0,1.0])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sin(Taylor(1,[0,0]))" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "cos (generic function with 12 methods)" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function cos(f::Taylor) #creando la funcion coseno, semejante a la del seno\n", + " S=zeros(length(f.coff))\n", + " C=zeros(length(f.coff))\n", + " C[1]=cos(f.coff[1])\n", + " S[2]=C[1]\n", + " m=0\n", + " for i in 2:length(f.coff)-1\n", + " for j in 0:i-1\n", + " m+=(i-j)*f(f.coff[i+1-j])*(C[j+1])\n", + " end\n", + " C[i+1]=-(m/i)\n", + " end\n", + " return Taylor(length(C)-1,C)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Taylor{Float64}(1,[1.0,0.0])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cos(Taylor(1,[0,0]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Las pruebas de estas pruebas se agregaron en el archivo \"runtest_taylor.jl\"" + ] + } + ], + "metadata": { + "anaconda-cloud": {}, + "kernelspec": { + "display_name": "Julia 0.5.0", + "language": "julia", + "name": "julia-0.5" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "0.5.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/tareas/Carlos/runtest_taylor.jl b/tareas/Carlos/runtest_taylor.jl new file mode 100644 index 0000000..a228ce4 --- /dev/null +++ b/tareas/Carlos/runtest_taylor.jl @@ -0,0 +1,20 @@ +include("AutomTaylor.jl") +using Base.Test +using BD +#las pruebas necesarias para taylor. +@testset "pruebas realizadas" begin + @test Taylor(0,[1])+Taylor(1,[1,2])==Taylor(1,[2,2]) #sumas + @test Taylor(1,[1,1])+Taylor(1,[1,1])==Taylor(1,[2,2]) + @test Taylor(0,[1])-Taylor(1,[1,2])==Taylor(1,[0,-2])#restas + @test Taylor(1,[1,1])-Taylor(1,[1,1])==Taylor(1,[0,0]) + @test 3*Taylor(2,[10,20,30])==Taylor(2,[30,60,90])#producto + @test Taylor(1,[1,1])*Taylor(1,[1,1])==Taylor(2,[1,2,1]) + @test Taylor(3,[2,4,6,8])/2==Taylor(3,[1,2,3,4])# divisiones + @test Taylor(2,[1,2,1])/Taylor(1,[1,1])==Taylor(1,[1,1]) + @test Taylor(1,[1,2])==Taylor(1,[1,2]) #igualdad + @test exp(Taylor(0,[0]))==Taylor(0,[1.0]) #exponencial + @test (Taylor(1,[2,0]))^2==Taylor(1,[4,0]) #potencia + @test sin(Taylor(1,[0,0]))==Taylor(1,[0,1])#seno + @test cos(Taylor(1,[0,0]))==Taylor(1,[1,0])#coseno + @test log(Taylor(1,[1,1]))==Taylor(1,[0,1])#logaritmo +end \ No newline at end of file