| 1 | =================== |
| 2 | Uma Turnê pelo Sage |
| 3 | =================== |
| 4 | |
| 5 | Esta apresentação ao Sage segue de perto o "tour do Mathematica" que |
| 6 | se encontra no começo do "manual do Mathematica". |
| 7 | |
| 8 | |
| 9 | Sage como uma Calculadora |
| 10 | ========================= |
| 11 | |
| 12 | A linha de comando do Sage possui o prompt ``sage:``; você não precisa |
| 13 | digitar essa palavra. Se você usar o Sage Notebook, então você deve |
| 14 | copiar todo o comando após o prompt ``sage:`` em uma célula, e |
| 15 | pressionar shift-enter para calcular o resultado. |
| 16 | |
| 17 | :: |
| 18 | |
| 19 | sage: 3 + 5 |
| 20 | 8 |
| 21 | |
| 22 | O acento circunflexo significa "elevar à potência". |
| 23 | |
| 24 | :: |
| 25 | |
| 26 | sage: 57.1 ^ 100 |
| 27 | 4.60904368661396e175 |
| 28 | |
| 29 | Pode-se calcular a inversa de uma matrix :math:`2 \times 2` com o Sage. |
| 30 | |
| 31 | :: |
| 32 | |
| 33 | sage: matrix([[1,2], [3,4]])^(-1) |
| 34 | [ -2 1] |
| 35 | [ 3/2 -1/2] |
| 36 | |
| 37 | A seguir, calculamos a integral de uma função simples. |
| 38 | |
| 39 | :: |
| 40 | |
| 41 | sage: x = var('x') # create a symbolic variable |
| 42 | sage: integrate(sqrt(x)*sqrt(1+x), x) |
| 43 | 1/4*((x + 1)^(3/2)/x^(3/2) + sqrt(x + 1)/sqrt(x))/((x + 1)^2/x^2 - 2*(x + 1)/x + 1) + 1/8*log(sqrt(x + 1)/sqrt(x) - 1) - 1/8*log(sqrt(x + 1)/sqrt(x) + 1) |
| 44 | |
| 45 | Agora vamos resolver uma equação quadrática com o Sage. O símbolo |
| 46 | ``==`` representa igualdade no Sage. |
| 47 | |
| 48 | :: |
| 49 | |
| 50 | sage: a = var('a') |
| 51 | sage: S = solve(x^2 + x == a, x); S |
| 52 | [x == -1/2*sqrt(4*a + 1) - 1/2, x == 1/2*sqrt(4*a + 1) - 1/2] |
| 53 | |
| 54 | O resultado é uma lista de igualdades. |
| 55 | |
| 56 | .. link |
| 57 | |
| 58 | :: |
| 59 | |
| 60 | sage: S[0].rhs() |
| 61 | -1/2*sqrt(4*a + 1) - 1/2 |
| 62 | sage: show(plot(sin(x) + sin(1.6*x), 0, 40)) |
| 63 | |
| 64 | .. image:: sin_plot.* |
| 65 | |
| 66 | |
| 67 | Cálculo Numérico com o Sage |
| 68 | =========================== |
| 69 | |
| 70 | Primeiro vamos criar uma matriz :math:`500 \times 500` de números aleatórios. |
| 71 | |
| 72 | :: |
| 73 | |
| 74 | sage: m = random_matrix(RDF,500) |
| 75 | |
| 76 | Leva alguns segundos para calcular os autovalores dessa matriz e |
| 77 | representá-los em um gráfico. |
| 78 | |
| 79 | .. link |
| 80 | |
| 81 | :: |
| 82 | |
| 83 | sage: e = m.eigenvalues() #about 2 seconds |
| 84 | sage: w = [(i, abs(e[i])) for i in range(len(e))] |
| 85 | sage: show(points(w)) |
| 86 | |
| 87 | .. image:: eigen_plot.* |
| 88 | |
| 89 | |
| 90 | Graças à biblioteca GMP (GNU Multiprecision Library), o Sage pode |
| 91 | efetuar cálculos com números muito grandes, até mesmo com números com |
| 92 | milhões de dígitos. |
| 93 | |
| 94 | :: |
| 95 | |
| 96 | sage: factorial(100) |
| 97 | 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 |
| 98 | sage: n = factorial(1000000) #about 2.5 seconds |
| 99 | |
| 100 | Vamos calcular :math:`\pi` com 100 algarismos decimais. |
| 101 | |
| 102 | :: |
| 103 | |
| 104 | sage: N(pi, digits=100) |
| 105 | 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 |
| 106 | |
| 107 | Agora o Sage vai fatorar um polinômio em duas variáveis. |
| 108 | |
| 109 | :: |
| 110 | |
| 111 | sage: R.<x,y> = QQ[] |
| 112 | sage: F = factor(x^99 + y^99) |
| 113 | sage: F |
| 114 | (x + y) * (x^2 - x*y + y^2) * (x^6 - x^3*y^3 + y^6) * |
| 115 | (x^10 - x^9*y + x^8*y^2 - x^7*y^3 + x^6*y^4 - x^5*y^5 + |
| 116 | x^4*y^6 - x^3*y^7 + x^2*y^8 - x*y^9 + y^10) * |
| 117 | (x^20 + x^19*y - x^17*y^3 - x^16*y^4 + x^14*y^6 + x^13*y^7 - |
| 118 | x^11*y^9 - x^10*y^10 - x^9*y^11 + x^7*y^13 + x^6*y^14 - |
| 119 | x^4*y^16 - x^3*y^17 + x*y^19 + y^20) * (x^60 + x^57*y^3 - |
| 120 | x^51*y^9 - x^48*y^12 + x^42*y^18 + x^39*y^21 - x^33*y^27 - |
| 121 | x^30*y^30 - x^27*y^33 + x^21*y^39 + x^18*y^42 - x^12*y^48 - |
| 122 | x^9*y^51 + x^3*y^57 + y^60) |
| 123 | sage: F.expand() |
| 124 | x^99 + y^99 |
| 125 | |
| 126 | O Sage leva menos de 5 segundos para calcular de quantas maneiras pode-se |
| 127 | particionar :math:`10^8` como uma soma de inteiros positivos. |
| 128 | |
| 129 | :: |
| 130 | |
| 131 | sage: z = Partitions(10^8).cardinality() #about 4.5 seconds |
| 132 | sage: str(z)[:40] |
| 133 | '1760517045946249141360373894679135204009' |
| 134 | |
| 135 | Algoritmos incluídos no Sage |
| 136 | ============================ |
| 137 | |
| 138 | Quando você usa o Sage, você acessa uma das maiores coleções |
| 139 | disponíveis de algoritmos computacionais de código aberto. |