Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active July 4, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genkuroki/6bcfa3c7fce417a4eae1bc31f243cdad to your computer and use it in GitHub Desktop.
Save genkuroki/6bcfa3c7fce417a4eae1bc31f243cdad to your computer and use it in GitHub Desktop.
Powers
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(x, k) = x^k",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "f (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(3, 2)",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "9"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(3, -2)",
"execution_count": 3,
"outputs": [
{
"output_type": "error",
"ename": "DomainError",
"evalue": "DomainError with -2:\nCannot raise an integer x to a negative power -2.\nMake x or -2 a float by adding a zero decimal (e.g., 2.0^-2 or 2^-2.0 instead of 2^-2), or write 1/x^2, float(x)^-2, x^float(-2) or (x//1)^-2",
"traceback": [
"DomainError with -2:\nCannot raise an integer x to a negative power -2.\nMake x or -2 a float by adding a zero decimal (e.g., 2.0^-2 or 2^-2.0 instead of 2^-2), or write 1/x^2, float(x)^-2, x^float(-2) or (x//1)^-2",
"",
"Stacktrace:",
" [1] throw_domerr_powbysq(::Int64, ::Int64) at .\\intfuncs.jl:193",
" [2] power_by_squaring(::Int64, ::Int64) at .\\intfuncs.jl:214",
" [3] ^ at .\\intfuncs.jl:238 [inlined]",
" [4] f(::Int64, ::Int64) at .\\In[1]:1",
" [5] top-level scope at In[3]:1"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(x, k) = typeof(inv(one(x)))(x)^k",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "g (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(3, -2)",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "0.1111111111111111"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(0, -2)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "Inf"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(3//1, -2)",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "1//9"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(0//1, -2)",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "1//0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "A = [\n 1 2\n 3 4\n]\ninv(A)",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "2×2 Array{Float64,2}:\n -2.0 1.0\n 1.5 -0.5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(A, 2)",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "2×2 Array{Int64,2}:\n 7 10\n 15 22"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(A, 2)",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "2×2 Array{Float64,2}:\n 7.0 10.0\n 15.0 22.0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "f(A, -2)",
"execution_count": 12,
"outputs": [
{
"output_type": "error",
"ename": "DomainError",
"evalue": "DomainError with -2:\nCannot raise an integer matrix x to a negative power -2.\nMake x a float matrix by adding a zero decimal (e.g., [2.0 1.0;1.0 0.0]^-2 instead of [2 1;1 0]^-2), or write float(x)^-2 or Rational.(x)^-2",
"traceback": [
"DomainError with -2:\nCannot raise an integer matrix x to a negative power -2.\nMake x a float matrix by adding a zero decimal (e.g., [2.0 1.0;1.0 0.0]^-2 instead of [2 1;1 0]^-2), or write float(x)^-2 or Rational.(x)^-2",
"",
"Stacktrace:",
" [1] throw_domerr_powbysq(::Array{Int64,2}, ::Int64) at .\\intfuncs.jl:198",
" [2] power_by_squaring(::Array{Int64,2}, ::Int64) at .\\intfuncs.jl:214",
" [3] ^ at D:\\buildbot\\worker\\package_win64\\build\\usr\\share\\julia\\stdlib\\v1.4\\LinearAlgebra\\src\\dense.jl:409 [inlined]",
" [4] f(::Array{Int64,2}, ::Int64) at .\\In[1]:1",
" [5] top-level scope at In[12]:1"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "g(A, -2)",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "2×2 Array{Float64,2}:\n 5.5 -2.5\n -3.75 1.75"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using SymPy\n@vars a\ng(a, -2)",
"execution_count": 14,
"outputs": [
{
"output_type": "error",
"ename": "DomainError",
"evalue": "DomainError with -2:\nCannot raise an integer x to a negative power -2.\nConvert input to float.",
"traceback": [
"DomainError with -2:\nCannot raise an integer x to a negative power -2.\nConvert input to float.",
"",
"Stacktrace:",
" [1] throw_domerr_powbysq(::Sym, ::Int64) at .\\intfuncs.jl:190",
" [2] power_by_squaring(::Sym, ::Int64) at .\\intfuncs.jl:214",
" [3] ^(::Sym, ::Int64) at .\\intfuncs.jl:239",
" [4] g(::Sym, ::Int64) at .\\In[4]:1",
" [5] top-level scope at In[14]:3"
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Base.:^(x::Sym, y::Integer) = x^Sym(y)\ng(a, -2)",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "1 \n--\n 2\na ",
"text/latex": "\\begin{equation*}\\frac{1}{a^{2}}\\end{equation*}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.4",
"display_name": "Julia 1.4.2",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.4.2"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "6bcfa3c7fce417a4eae1bc31f243cdad",
"data": {
"description": "Powers",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/6bcfa3c7fce417a4eae1bc31f243cdad"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment