Skip to content

Instantly share code, notes, and snippets.

@Ismael-VC
Last active August 29, 2015 13:57
Show Gist options
  • Save Ismael-VC/9562856 to your computer and use it in GitHub Desktop.
Save Ismael-VC/9562856 to your computer and use it in GitHub Desktop.
Números complejos con Python.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Algebra Lineal\n",
"\n",
"Ismael Venegas Castell\u00f3: <[email protected]>\n",
"## N\u00fameros complejos.\n",
"\n",
"###14-III-14\n",
"\n",
"#### Acerca de numpy: [Numpy](http://es.wikipedia.org/wiki/NumPy) "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Primero importamos el m\u00f3dulo \"numpy\" (\"python n\u00famerico\", del ingles 'numerical python')\n",
"# bajo el alias \"np\":\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Ahora utilizamos la funci\u00f3n \"np.sqrt\" (\"raiz cuadrada\", del ingles 'square root')\n",
"# del m\u00f3dulo \"np\" utilizando la notaci\u00f3n \"modulo.funci\u00f3n\" ,\n",
"# usando el n\u00famero complejo/imaginario -1 (en Python \"j\" denota el n\u00famero imaginario \"i\"):\n",
"np.sqrt(complex(-1))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
"1j"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"|Potencia|Desarrollo|Equivalencia|\n",
"|--------|----------|------------|\n",
"|$i^1$|$\\sqrt{-1}$|$i$|\n",
"|$i^2$|$(\\sqrt{-1})(\\sqrt{-1})=(\\sqrt{-1})^2$|$-1$|\n",
"|$i^3$|$i^2\u00b7i=(-1)(\\sqrt{-1})$|$-i$|\n",
"|$i^4$|$i^2\u00b7i^2=(-1)(-1)$|$1$|\n",
"|$i^5$|$i^4\u00b7i=(1)(i)$|$i$|\n",
"|$i^6$|$i^4\u00b7i^2=(1)(-1)$|$-1$|"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Para encontrar $i$ elevado a cualquier potencia, hay que dividir la potencia entre 4, ya que $i^4=1$. El residuo va a ser la potencia a la cual se eleve $i$ y se multiplique por 1, ejemplo:\n",
"\n",
"$i^8$\n",
"\n",
"$\\frac{8}{4} = (i^2)^4 i^0 = (1)(1)=1$ \n",
"\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# La funci\u00f3n \"divmod\" regresa una tupla con el cociente y dividendo de una divisi\u00f3n:\n",
"cociente, residuo = divmod(8, 4) # \"divmod\" nos da el dividendo y el modulo (residuo) de 8/4.\n",
"print('cociente: {0}\\n'\n",
" ' residuo: {1}'.format(cociente, residuo))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"cociente: 2\n",
" residuo: 0\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Representaci\u00f3n binomica o cartesiana.\n",
"\n",
"$a+bi$\n",
"\n",
"Donde $a$ es la parte real y $bi$ es la parte imaginaria.\n",
"Suponiendo que:\n",
"\n",
"* $2x^2-2x+5=0$\n",
"\n",
"Usando la formula general:\n",
"\n",
"$x = \\frac{-b \\pm \\sqrt{b\u00b2-4ac}}{2a}$"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Despues definimos una funci\u00f3n que compute la formula general,\n",
"# \"fgen\" regresa una tupla con el valor de (x1, x2),\n",
"# tenemos que usar la funcion \"complex\" para tener un\n",
"# numero complejo en caso de que la raiz sea negativa:\n",
"def fgen(a, b, c):\n",
" x1 = (-b + np.sqrt(complex(b**2 - 4*a*c))) / (2*a)\n",
" x2 = (-b - np.sqrt(complex(b**2 - 4*a*c))) / (2*a)\n",
" return (x1, x2)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Por ultimo llamamos a la funci\u00f3n \"fgen\" con los argumentos (2, -2, 5).\n",
"# a = 2; b = -2; c = 5:\n",
"x1, x2 = fgen(2, -2, 5)\n",
"\n",
"print('x1 = {0}\\n'\n",
" 'x2 = {1}'.format(x1, x2))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"x1 = (0.5+1.5j)\n",
"x2 = (0.5-1.5j)\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Es decir, el resultado es: \n",
"\n",
"$x_1 = \\frac{1}{2} + \\frac{3}{2}i$\n",
"\n",
"$x_2 = \\frac{1}{2} - \\frac{3}{2}i$"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment