julia_coursera/04_Coursera_1.ipynb

1103 lines
31 KiB
Plaintext
Raw Normal View History

2020-05-17 04:38:06 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n"
]
}
],
"source": [
"greeting = \"Hello world\"\n",
"println(greeting)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.9800665778412416, 2.302585092994046, 1.22)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a,b,c = cos(0.2), log(10), abs(-1.22)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9800665778412416"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"search: \u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22m \u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22mh \u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22md \u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22mc \u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22mpi a\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22m a\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22mh a\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22md sin\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22m \u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22mn\u001b[0m\u001b[1ms\u001b[22mt \u001b[0m\u001b[1mc\u001b[22ml\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1ms\u001b[22me is\u001b[0m\u001b[1mc\u001b[22m\u001b[0m\u001b[1mo\u001b[22mn\u001b[0m\u001b[1ms\u001b[22mt\n",
"\n"
]
},
{
"data": {
"text/latex": [
"\\begin{verbatim}\n",
"cos(x)\n",
"\\end{verbatim}\n",
"Compute cosine of \\texttt{x}, where \\texttt{x} is in radians.\n",
"\n",
"\\rule{\\textwidth}{1pt}\n",
"\\begin{verbatim}\n",
"cos(A::AbstractMatrix)\n",
"\\end{verbatim}\n",
"Compute the matrix cosine of a square matrix \\texttt{A}.\n",
"\n",
"If \\texttt{A} is symmetric or Hermitian, its eigendecomposition (\\href{@ref}{\\texttt{eigen}}) is used to compute the cosine. Otherwise, the cosine is determined by calling \\href{@ref}{\\texttt{exp}}.\n",
"\n",
"\\section{Examples}\n",
"\\begin{verbatim}\n",
"julia> cos(fill(1.0, (2,2)))\n",
"2×2 Array{Float64,2}:\n",
" 0.291927 -0.708073\n",
" -0.708073 0.291927\n",
"\\end{verbatim}\n"
],
"text/markdown": [
"```\n",
"cos(x)\n",
"```\n",
"\n",
"Compute cosine of `x`, where `x` is in radians.\n",
"\n",
"---\n",
"\n",
"```\n",
"cos(A::AbstractMatrix)\n",
"```\n",
"\n",
"Compute the matrix cosine of a square matrix `A`.\n",
"\n",
"If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](@ref)) is used to compute the cosine. Otherwise, the cosine is determined by calling [`exp`](@ref).\n",
"\n",
"# Examples\n",
"\n",
"```jldoctest\n",
"julia> cos(fill(1.0, (2,2)))\n",
"2×2 Array{Float64,2}:\n",
" 0.291927 -0.708073\n",
" -0.708073 0.291927\n",
"```\n"
],
"text/plain": [
"\u001b[36m cos(x)\u001b[39m\n",
"\n",
" Compute cosine of \u001b[36mx\u001b[39m, where \u001b[36mx\u001b[39m is in radians.\n",
"\n",
" ────────────────────────────────────────────────────────────────────────────\n",
"\n",
"\u001b[36m cos(A::AbstractMatrix)\u001b[39m\n",
"\n",
" Compute the matrix cosine of a square matrix \u001b[36mA\u001b[39m.\n",
"\n",
" If \u001b[36mA\u001b[39m is symmetric or Hermitian, its eigendecomposition (\u001b[36meigen\u001b[39m) is used to\n",
" compute the cosine. Otherwise, the cosine is determined by calling \u001b[36mexp\u001b[39m.\n",
"\n",
"\u001b[1m Examples\u001b[22m\n",
"\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n",
"\n",
"\u001b[36m julia> cos(fill(1.0, (2,2)))\u001b[39m\n",
"\u001b[36m 2×2 Array{Float64,2}:\u001b[39m\n",
"\u001b[36m 0.291927 -0.708073\u001b[39m\n",
"\u001b[36m -0.708073 0.291927\u001b[39m"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"? cos"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cos(0)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cos(π)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6.123233995736766e-17"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cos(π/2)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Irrational{:π}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"typeof(π)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"12 methods for generic function <b>muladd</b>:<ul><li> muladd(a::<b>Float16</b>, b::<b>Float16</b>, c::<b>Float16</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/float.jl#L406\" target=\"_blank\">float.jl:406</a></li> <li> muladd(x::<b>Float64</b>, y::<b>Float64</b>, z::<b>Float64</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/float.jl#L404\" target=\"_blank\">float.jl:404</a></li> <li> muladd(x::<b>Float32</b>, y::<b>Float32</b>, z::<b>Float32</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/float.jl#L403\" target=\"_blank\">float.jl:403</a></li> <li> muladd(z::<b>Complex</b>, w::<b>Complex</b>, x::<b>Complex</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L272\" target=\"_blank\">complex.jl:272</a></li> <li> muladd(x::<b>Real</b>, z::<b>Complex</b>, y::<b>Number</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L315\" target=\"_blank\">complex.jl:315</a></li> <li> muladd(z::<b>Complex</b>, x::<b>Real</b>, y::<b>Real</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L316\" target=\"_blank\">complex.jl:316</a></li> <li> muladd(z::<b>Complex</b>, x::<b>Real</b>, w::<b>Complex</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L317\" target=\"_blank\">complex.jl:317</a></li> <li> muladd(x::<b>Real</b>, y::<b>Real</b>, z::<b>Complex</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L319\" target=\"_blank\">complex.jl:319</a></li> <li> muladd(z::<b>Complex</b>, w::<b>Complex</b>, x::<b>Real</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/complex.jl#L320\" target=\"_blank\">complex.jl:320</a></li> <li> muladd(x::<b>T</b>, y::<b>T</b>, z::<b>T</b>)<i> where T<:Number</i> in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/promotion.jl#L397\" target=\"_blank\">promotion.jl:397</a></li> <li> muladd(x::<b>Number</b>, y::<b>Number</b>, z::<b>Number</b>) in Base at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/promotion.jl#L348\" target=\"_blank\">promotion.jl:348</a></li> <li> muladd(x, y, z) in Base.Math at <a href=\"https://github.com/JuliaLang/julia/tree/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/math.jl#L1012\" target=\"_blank\">math.jl:1012</a></li> </ul>"
],
"text/plain": [
"# 12 methods for generic function \"muladd\":\n",
"[1] muladd(a::Float16, b::Float16, c::Float16) in Base at float.jl:406\n",
"[2] muladd(x::Float64, y::Float64, z::Float64) in Base at float.jl:404\n",
"[3] muladd(x::Float32, y::Float32, z::Float32) in Base at float.jl:403\n",
"[4] muladd(z::Complex, w::Complex, x::Complex) in Base at complex.jl:272\n",
"[5] muladd(x::Real, z::Complex, y::Number) in Base at complex.jl:315\n",
"[6] muladd(z::Complex, x::Real, y::Real) in Base at complex.jl:316\n",
"[7] muladd(z::Complex, x::Real, w::Complex) in Base at complex.jl:317\n",
"[8] muladd(x::Real, y::Real, z::Complex) in Base at complex.jl:319\n",
"[9] muladd(z::Complex, w::Complex, x::Real) in Base at complex.jl:320\n",
"[10] muladd(x::T, y::T, z::T) where T<:Number in Base at promotion.jl:397\n",
"[11] muladd(x::Number, y::Number, z::Number) in Base at promotion.jl:348\n",
"[12] muladd(x, y, z) in Base.Math at math.jl:1012"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"methods(muladd)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"myfunc (generic function with 1 method)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myfunc(firstvar) = 20* firstvar"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6664.4439999999995"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myfunc(333.2222)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"addxtoy (generic function with 1 method)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"addxtoy(x,y) = x + y"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"18.8"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"addxtoy(22,-3.2)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"nextfunc (generic function with 1 method)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function nextfunc(x,y,z)\n",
" x+y*z\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"showdebugprintln (generic function with 1 method)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function showdebugprintln(testvar)\n",
" println(\"Show inside the showdebugprintln() now\")\n",
" println(\"The type of testvar is $(typeof(testvar)) and the value of testvar is $testvar\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2-element Array{Any,1}:\n",
" '1'\n",
" 2.0 "
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = ['1',2.]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Show inside the showdebugprintln() now\n",
"The type of testvar is Array{Any,1} and the value of testvar is Any['1', 2.0]\n"
]
}
],
"source": [
"showdebugprintln(a)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mycos (generic function with 1 method)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycos(x) = cos(x)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.955336489125606"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycos(.3)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mycos (generic function with 2 methods)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycos(adj,hyp) = adj/hyp"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9230769230769231"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycos(12,13)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"2 methods for generic function <b>mycos</b>:<ul><li> mycos(x) in Main at In[28]:1</li> <li> mycos(adj, hyp) in Main at In[30]:1</li> </ul>"
],
"text/plain": [
"# 2 methods for generic function \"mycos\":\n",
"[1] mycos(x) in Main at In[28]:1\n",
"[2] mycos(adj, hyp) in Main at In[30]:1"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"methods(mycos)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mycos (generic function with 3 methods)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycos(thet::Float64) = cos(thet)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"84.0"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"60 \\ 5040"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"search: \u001b[0m\u001b[1m\\\u001b[22m\n",
"\n"
]
},
{
"data": {
"text/latex": [
"\\begin{verbatim}\n",
"\\(x, y)\n",
"\\end{verbatim}\n",
"Left division operator: multiplication of \\texttt{y} by the inverse of \\texttt{x} on the left. Gives floating-point results for integer arguments.\n",
"\n",
"\\section{Examples}\n",
"\\begin{verbatim}\n",
"julia> 3 \\ 6\n",
"2.0\n",
"\n",
"julia> inv(3) * 6\n",
"2.0\n",
"\n",
"julia> A = [4 3; 2 1]; x = [5, 6];\n",
"\n",
"julia> A \\ x\n",
"2-element Array{Float64,1}:\n",
" 6.5\n",
" -7.0\n",
"\n",
"julia> inv(A) * x\n",
"2-element Array{Float64,1}:\n",
" 6.5\n",
" -7.0\n",
"\\end{verbatim}\n",
"\\rule{\\textwidth}{1pt}\n",
"\\begin{verbatim}\n",
"\\(A, B)\n",
"\\end{verbatim}\n",
"Matrix division using a polyalgorithm. For input matrices \\texttt{A} and \\texttt{B}, the result \\texttt{X} is such that \\texttt{A*X == B} when \\texttt{A} is square. The solver that is used depends upon the structure of \\texttt{A}. If \\texttt{A} is upper or lower triangular (or diagonal), no factorization of \\texttt{A} is required and the system is solved with either forward or backward substitution. For non-triangular square matrices, an LU factorization is used.\n",
"\n",
"For rectangular \\texttt{A} the result is the minimum-norm least squares solution computed by a pivoted QR factorization of \\texttt{A} and a rank estimate of \\texttt{A} based on the R factor.\n",
"\n",
"When \\texttt{A} is sparse, a similar polyalgorithm is used. For indefinite matrices, the \\texttt{LDLt} factorization does not use pivoting during the numerical factorization and therefore the procedure can fail even for invertible matrices.\n",
"\n",
"\\section{Examples}\n",
"\\begin{verbatim}\n",
"julia> A = [1 0; 1 -2]; B = [32; -4];\n",
"\n",
"julia> X = A \\ B\n",
"2-element Array{Float64,1}:\n",
" 32.0\n",
" 18.0\n",
"\n",
"julia> A * X == B\n",
"true\n",
"\\end{verbatim}\n",
"\\rule{\\textwidth}{1pt}\n",
"\\begin{verbatim}\n",
"(\\)(F::QRSparse, B::StridedVecOrMat)\n",
"\\end{verbatim}\n",
"Solve the least squares problem $\\min\\|Ax - b\\|^2$ or the linear system of equations $Ax=b$ when \\texttt{F} is the sparse QR factorization of $A$. A basic solution is returned when the problem is underdetermined.\n",
"\n",
"\\section{Examples}\n",
"\\begin{verbatim}\n",
"julia> A = sparse([1,2,4], [1,1,1], [1.0,1.0,1.0], 4, 2)\n",
"4×2 SparseMatrixCSC{Float64,Int64} with 3 stored entries:\n",
" [1, 1] = 1.0\n",
" [2, 1] = 1.0\n",
" [4, 1] = 1.0\n",
"\n",
"julia> qr(A)\\fill(1.0, 4)\n",
"2-element Array{Float64,1}:\n",
" 1.0\n",
" 0.0\n",
"\\end{verbatim}\n"
],
"text/markdown": [
"```\n",
"\\(x, y)\n",
"```\n",
"\n",
"Left division operator: multiplication of `y` by the inverse of `x` on the left. Gives floating-point results for integer arguments.\n",
"\n",
"# Examples\n",
"\n",
"```jldoctest\n",
"julia> 3 \\ 6\n",
"2.0\n",
"\n",
"julia> inv(3) * 6\n",
"2.0\n",
"\n",
"julia> A = [4 3; 2 1]; x = [5, 6];\n",
"\n",
"julia> A \\ x\n",
"2-element Array{Float64,1}:\n",
" 6.5\n",
" -7.0\n",
"\n",
"julia> inv(A) * x\n",
"2-element Array{Float64,1}:\n",
" 6.5\n",
" -7.0\n",
"```\n",
"\n",
"---\n",
"\n",
"```\n",
"\\(A, B)\n",
"```\n",
"\n",
"Matrix division using a polyalgorithm. For input matrices `A` and `B`, the result `X` is such that `A*X == B` when `A` is square. The solver that is used depends upon the structure of `A`. If `A` is upper or lower triangular (or diagonal), no factorization of `A` is required and the system is solved with either forward or backward substitution. For non-triangular square matrices, an LU factorization is used.\n",
"\n",
"For rectangular `A` the result is the minimum-norm least squares solution computed by a pivoted QR factorization of `A` and a rank estimate of `A` based on the R factor.\n",
"\n",
"When `A` is sparse, a similar polyalgorithm is used. For indefinite matrices, the `LDLt` factorization does not use pivoting during the numerical factorization and therefore the procedure can fail even for invertible matrices.\n",
"\n",
"# Examples\n",
"\n",
"```jldoctest\n",
"julia> A = [1 0; 1 -2]; B = [32; -4];\n",
"\n",
"julia> X = A \\ B\n",
"2-element Array{Float64,1}:\n",
" 32.0\n",
" 18.0\n",
"\n",
"julia> A * X == B\n",
"true\n",
"```\n",
"\n",
"---\n",
"\n",
"```\n",
"(\\)(F::QRSparse, B::StridedVecOrMat)\n",
"```\n",
"\n",
"Solve the least squares problem $\\min\\|Ax - b\\|^2$ or the linear system of equations $Ax=b$ when `F` is the sparse QR factorization of $A$. A basic solution is returned when the problem is underdetermined.\n",
"\n",
"# Examples\n",
"\n",
"```jldoctest\n",
"julia> A = sparse([1,2,4], [1,1,1], [1.0,1.0,1.0], 4, 2)\n",
"4×2 SparseMatrixCSC{Float64,Int64} with 3 stored entries:\n",
" [1, 1] = 1.0\n",
" [2, 1] = 1.0\n",
" [4, 1] = 1.0\n",
"\n",
"julia> qr(A)\\fill(1.0, 4)\n",
"2-element Array{Float64,1}:\n",
" 1.0\n",
" 0.0\n",
"```\n"
],
"text/plain": [
"\u001b[36m \\(x, y)\u001b[39m\n",
"\n",
" Left division operator: multiplication of \u001b[36my\u001b[39m by the inverse of \u001b[36mx\u001b[39m on the left.\n",
" Gives floating-point results for integer arguments.\n",
"\n",
"\u001b[1m Examples\u001b[22m\n",
"\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n",
"\n",
"\u001b[36m julia> 3 \\ 6\u001b[39m\n",
"\u001b[36m 2.0\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> inv(3) * 6\u001b[39m\n",
"\u001b[36m 2.0\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> A = [4 3; 2 1]; x = [5, 6];\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> A \\ x\u001b[39m\n",
"\u001b[36m 2-element Array{Float64,1}:\u001b[39m\n",
"\u001b[36m 6.5\u001b[39m\n",
"\u001b[36m -7.0\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> inv(A) * x\u001b[39m\n",
"\u001b[36m 2-element Array{Float64,1}:\u001b[39m\n",
"\u001b[36m 6.5\u001b[39m\n",
"\u001b[36m -7.0\u001b[39m\n",
"\n",
" ────────────────────────────────────────────────────────────────────────────\n",
"\n",
"\u001b[36m \\(A, B)\u001b[39m\n",
"\n",
" Matrix division using a polyalgorithm. For input matrices \u001b[36mA\u001b[39m and \u001b[36mB\u001b[39m, the\n",
" result \u001b[36mX\u001b[39m is such that \u001b[36mA*X == B\u001b[39m when \u001b[36mA\u001b[39m is square. The solver that is used\n",
" depends upon the structure of \u001b[36mA\u001b[39m. If \u001b[36mA\u001b[39m is upper or lower triangular (or\n",
" diagonal), no factorization of \u001b[36mA\u001b[39m is required and the system is solved with\n",
" either forward or backward substitution. For non-triangular square matrices,\n",
" an LU factorization is used.\n",
"\n",
" For rectangular \u001b[36mA\u001b[39m the result is the minimum-norm least squares solution\n",
" computed by a pivoted QR factorization of \u001b[36mA\u001b[39m and a rank estimate of \u001b[36mA\u001b[39m based\n",
" on the R factor.\n",
"\n",
" When \u001b[36mA\u001b[39m is sparse, a similar polyalgorithm is used. For indefinite matrices,\n",
" the \u001b[36mLDLt\u001b[39m factorization does not use pivoting during the numerical\n",
" factorization and therefore the procedure can fail even for invertible\n",
" matrices.\n",
"\n",
"\u001b[1m Examples\u001b[22m\n",
"\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n",
"\n",
"\u001b[36m julia> A = [1 0; 1 -2]; B = [32; -4];\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> X = A \\ B\u001b[39m\n",
"\u001b[36m 2-element Array{Float64,1}:\u001b[39m\n",
"\u001b[36m 32.0\u001b[39m\n",
"\u001b[36m 18.0\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> A * X == B\u001b[39m\n",
"\u001b[36m true\u001b[39m\n",
"\n",
" ────────────────────────────────────────────────────────────────────────────\n",
"\n",
"\u001b[36m (\\)(F::QRSparse, B::StridedVecOrMat)\u001b[39m\n",
"\n",
" Solve the least squares problem \u001b[35m\\min\\|Ax - b\\|^2\u001b[39m or the linear system of\n",
" equations \u001b[35mAx=b\u001b[39m when \u001b[36mF\u001b[39m is the sparse QR factorization of \u001b[35mA\u001b[39m. A basic solution\n",
" is returned when the problem is underdetermined.\n",
"\n",
"\u001b[1m Examples\u001b[22m\n",
"\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n",
"\n",
"\u001b[36m julia> A = sparse([1,2,4], [1,1,1], [1.0,1.0,1.0], 4, 2)\u001b[39m\n",
"\u001b[36m 4×2 SparseMatrixCSC{Float64,Int64} with 3 stored entries:\u001b[39m\n",
"\u001b[36m [1, 1] = 1.0\u001b[39m\n",
"\u001b[36m [2, 1] = 1.0\u001b[39m\n",
"\u001b[36m [4, 1] = 1.0\u001b[39m\n",
"\u001b[36m \u001b[39m\n",
"\u001b[36m julia> qr(A)\\fill(1.0, 4)\u001b[39m\n",
"\u001b[36m 2-element Array{Float64,1}:\u001b[39m\n",
"\u001b[36m 1.0\u001b[39m\n",
"\u001b[36m 0.0\u001b[39m"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"? \\"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"84.0"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5040/60"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"addone (generic function with 1 method)"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"addone(x::Int64) = x+1"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"ename": "MethodError",
"evalue": "MethodError: no method matching addone(::Float64)\nClosest candidates are:\n addone(!Matched::Int64) at In[38]:1",
"output_type": "error",
"traceback": [
"MethodError: no method matching addone(::Float64)\nClosest candidates are:\n addone(!Matched::Int64) at In[38]:1",
"",
"Stacktrace:",
" [1] top-level scope at In[42]:1"
]
}
],
"source": [
"addone(ceil(1.2)*10)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2)"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a,b=1,2"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 1)"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a,b=b,a"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22\n"
]
}
],
"source": [
"function test(input)\n",
" println(\"$input\"^2)\n",
"end\n",
"\n",
"test(2)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22\n"
]
},
{
"data": {
"text/plain": [
"Nothing"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"typeof(test(2))"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"add2 (generic function with 1 method)"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add2(x,y) = return x + y"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12\n"
]
}
],
"source": [
"println(add2(5, 7))"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"coordinates (generic function with 3 methods)"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function coordinates(x, y=0, z=0)\n",
" println(\"($x, $y, $z)\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 0, 2)\n"
]
}
],
"source": [
"coordinates(1,0,2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.2.0",
"language": "julia",
"name": "julia-1.2"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}