Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active March 5, 2021 06:52
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/9faec0c1114dea41b29ae35924d87054 to your computer and use it in GitHub Desktop.
Save genkuroki/9faec0c1114dea41b29ae35924d87054 to your computer and use it in GitHub Desktop.
struct Vec example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using LinearAlgebra\n\nstruct Vec{T} x::T; y::T; z::T end\n\nfor op in (:+, :-)\n @eval Base.$op(v::Vec) = Vec($op(v.x), $op(v.y), $op(v.z))\n @eval Base.$op(v::Vec, w::Vec) = Vec($op(v.x, w.x), $op(v.y, w.y), $op(v.z, w.z))\nend\n\nfor op in (:*, :/)\n @eval Base.$op(v::Vec, c) = Vec($op(v.x, c), $op(v.y, c), $op(v.z, c))\nend\n\nfor op in (:*, :\\)\n @eval Base.$op(c, v::Vec) = Vec($op(c, v.x), $op(c, v.y), $op(c, v.z))\nend\n\nLinearAlgebra.dot(v::Vec, w::Vec) = v.x*w.x + v.y*w.y + v.z*w.z\nLinearAlgebra.cross(v::Vec, w::Vec) = Vec(v.y*w.z-v.z*w.y, v.z*w.z-v.x*w.z, v.x*w.y-v.y*w.x)\n\nBase.Vector(v::Vec) = [v.x, v.y, v.z]",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v = Vec(1, 2, 3)\nw = Vec(4, 5, 6)\n[+v, -v, v+w, v-w]",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "4-element Vector{Vec{Int64}}:\n Vec{Int64}(1, 2, 3)\n Vec{Int64}(-1, -2, -3)\n Vec{Int64}(5, 7, 9)\n Vec{Int64}(-3, -3, -3)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[3v, v*3, v/3, 3\\v]",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "4-element Vector{Vec}:\n Vec{Int64}(3, 6, 9)\n Vec{Int64}(3, 6, 9)\n Vec{Float64}(0.3333333333333333, 0.6666666666666666, 1.0)\n Vec{Float64}(0.3333333333333333, 0.6666666666666666, 1.0)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[dot(v, w), v⋅w, Vector(v)⋅Vector(w)]",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "3-element Vector{Int64}:\n 32\n 32\n 32"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[cross(v, w), v×w, Vector(v)×Vector(w)]",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "3-element Vector{Any}:\n Vec{Int64}(-3, 12, -3)\n Vec{Int64}(-3, 12, -3)\n [-3, 6, -3]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "methods(cross)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "# 2 methods for generic function \"cross\":\n[1] cross(a::AbstractVector{T} where T, b::AbstractVector{T} where T) in LinearAlgebra at C:\\Julia-1.7.0-DEV\\share\\julia\\stdlib\\v1.7\\LinearAlgebra\\src\\generic.jl:310\n[2] cross(v::Vec, w::Vec) in Main at In[1]:19",
"text/html": "# 2 methods for generic function <b>cross</b>:<ul><li> cross(a::<b>AbstractVector{T} where T</b>, b::<b>AbstractVector{T} where T</b>) in LinearAlgebra at <a href=\"file://C:/buildbot/worker/package_win64/build/usr/share/julia/stdlib/v1.7/LinearAlgebra/src/generic.jl\" target=\"_blank\">C:\\Julia-1.7.0-DEV\\share\\julia\\stdlib\\v1.7\\LinearAlgebra\\src\\generic.jl:310</a></li> <li> cross(v::<b>Vec</b>, w::<b>Vec</b>) in Main at In[1]:19</li> </ul>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "*(v, 3)",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "Vec{Int64}(3, 6, 9)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.7-depwarn-o3",
"display_name": "Julia 1.7.0-DEV depwarn -O3",
"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.7.0"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "9faec0c1114dea41b29ae35924d87054",
"data": {
"description": "struct Vec example",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/9faec0c1114dea41b29ae35924d87054"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment