https://github.com/ohno/matrixprintf.jl
using Printf.jl to each element of a matrix
https://github.com/ohno/matrixprintf.jl
Last synced: 3 months ago
JSON representation
using Printf.jl to each element of a matrix
- Host: GitHub
- URL: https://github.com/ohno/matrixprintf.jl
- Owner: ohno
- License: mit
- Created: 2022-06-11T16:03:02.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-13T08:29:02.000Z (about 4 years ago)
- Last Synced: 2025-12-09T08:58:38.431Z (6 months ago)
- Language: Julia
- Size: 6.84 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.ipynb
- License: LICENSE
Awesome Lists containing this project
README
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MatrixPrintf\n",
"\n",
"[](https://travis-ci.com/ohno/MatrixPrintf.jl)\n",
"[](https://codecov.io/gh/ohno/MatrixPrintf.jl)\n",
"[](https://coveralls.io/github/ohno/MatrixPrintf.jl?branch=main)\n",
"\n",
"## Installation\n",
"\n",
"To add this package, run the following code in your Jupyter Notebook:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/ohno/MatrixPrintf.jl.git`\n",
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `C:\\Users\\Shuhei\\.julia\\registries\\General.toml`\n",
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `C:\\Users\\Shuhei\\.julia\\environments\\v1.7\\Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `C:\\Users\\Shuhei\\.julia\\environments\\v1.7\\Manifest.toml`\n"
]
}
],
"source": [
"using Pkg\n",
"Pkg.add(path=\"https://github.com/ohno/MatrixPrintf.jl.git\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or use `add` upon Pkg REPL:\n",
"\n",
"```julia\n",
"]\n",
"add https://github.com/ohno/MatrixPrintf.jl.git\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Usage\n",
"\n",
"To use this package, run the following code in your Jupyter Notebook or code:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using MatrixPrintf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Examples"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{Float64}:\n",
" 0.01 2.0\n",
" 3.0 -400.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = [1 2;\n",
" 3 4]\n",
"\n",
"B = [1 0;\n",
" 0 -1]\n",
"\n",
"C = [0.01 2;\n",
" 3.0 -400]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### `msprintf(M)`\n",
"\n",
"```julia:input\n",
"msprintf(A)\n",
"msprintf(B)\n",
"msprintf(C)\n",
"```\n",
"\n",
"```julia:output\n",
"[1.0 2.0;\n",
" 3.0 4.0]\n",
"[ 1.0 0.0;\n",
" 0.0 -1.0]\n",
"[ 0.0 2.0;\n",
" 3.0 -400.0]\n",
"```\n",
"\n",
"### `msprintf(M, symbol=\"M\")`\n",
"\n",
"```julia:input\n",
"msprintf(A, symbol=\"A\")\n",
"msprintf(B, symbol=\"B\")\n",
"msprintf(C, symbol=\"C\")\n",
"```\n",
"\n",
"```julia:output\n",
"A = [1.0 2.0;\n",
" 3.0 4.0]\n",
"B = [ 1.0 0.0;\n",
" 0.0 -1.0]\n",
"C = [ 0.0 2.0;\n",
" 3.0 -400.0]\n",
"```\n",
"\n",
"### `msprintf(M, symbol=\"M\", digit=5)`\n",
"\n",
"```julia:input\n",
"msprintf(A, symbol=\"A\", digit=5)\n",
"msprintf(B, symbol=\"B\", digit=5)\n",
"msprintf(C, symbol=\"C\", digit=5)\n",
"```\n",
"\n",
"```julia:output\n",
"A = [1.00000 2.00000;\n",
" 3.00000 4.00000]\n",
"B = [ 1.00000 0.00000;\n",
" 0.00000 -1.00000]\n",
"C = [ 0.01000 2.00000;\n",
" 3.00000 -400.00000]\n",
"```\n",
"\n",
"### `msprintf(M, symbol=\"M\", digit=2)`\n",
"\n",
"```julia:input\n",
"msprintf(A, symbol=\"A\", digit=2)\n",
"msprintf(B, symbol=\"B\", digit=2)\n",
"msprintf(C, symbol=\"C\", digit=2)\n",
"```\n",
"\n",
"```julia:output\n",
"A = [1.00 2.00;\n",
" 3.00 4.00]\n",
"B = [ 1.00 0.00;\n",
" 0.00 -1.00]\n",
"C = [ 0.01 2.00;\n",
" 3.00 -400.00]\n",
"```\n",
"\n",
"### `msprintf(M, symbol=\"M\", digit=0)`\n",
"\n",
"```julia:input\n",
"msprintf(A, symbol=\"A\", digit=0)\n",
"msprintf(B, symbol=\"B\", digit=0)\n",
"msprintf(C, symbol=\"C\", digit=0)\n",
"```\n",
"\n",
"```julia:output\n",
"A = [1 2;\n",
" 3 4]\n",
"B = [ 1 0;\n",
" 0 -1]\n",
"C = [ 0 2;\n",
" 3 -400]\n",
"```\n",
"\n",
"### `msprintf(M, symbol=\"M\", format=\"%d\")`\n",
"\n",
"```julia:input\n",
"msprintf(A, symbol=\"A\", format=\"%d\")\n",
"msprintf(B, symbol=\"B\", format=\"%d\")\n",
"msprintf(C, symbol=\"C\", format=\"%d\")\n",
"```\n",
"\n",
"```julia:output\n",
"A = [1 2;\n",
" 3 4]\n",
"B = [1 0;\n",
" 0 -1]\n",
"C = [0 2;\n",
" 3 -400]\n",
"```\n",
"\n",
"### `msprintf(M, symbol=\"M\", format=\"%8.1e\")`\n",
"\n",
"```julia:input\n",
"msprintf(A, symbol=\"A\", format=\"%8.1e\")\n",
"msprintf(B, symbol=\"B\", format=\"%8.1e\")\n",
"msprintf(C, symbol=\"C\", format=\"%8.1e\")\n",
"```\n",
"\n",
"```julia:output\n",
"A = [ 1.0e+00 2.0e+00;\n",
" 3.0e+00 4.0e+00]\n",
"B = [ 1.0e+00 0.0e+00;\n",
" 0.0e+00 -1.0e+00]\n",
"C = [ 1.0e-02 2.0e+00;\n",
" 3.0e+00 -4.0e+02]\n",
"```\n",
"\n"
]
}
],
"source": [
"function table(title, codes...)\n",
" f(code) = eval(Meta.parse(code))\n",
" X = codes\n",
" Y = f.(X)\n",
" \n",
" println(\"### `$(title)`\")\n",
" println(\"\\n```julia:input\")\n",
" for row in X\n",
" println(replace(row, \" \"=>\" \"))\n",
" end\n",
" println(\"```\")\n",
" println(\"\\n```julia:output\")\n",
" for row in Y\n",
" println(replace(row, \" \"=>\" \"))\n",
" end\n",
" println(\"```\\n\")\n",
" \n",
"end\n",
"\n",
"table(\"msprintf(M)\",\n",
" \"msprintf(A)\",\n",
" \"msprintf(B)\",\n",
" \"msprintf(C)\"\n",
")\n",
"\n",
"table(\"msprintf(M, symbol=\\\"M\\\")\",\n",
" \"msprintf(A, symbol=\\\"A\\\")\",\n",
" \"msprintf(B, symbol=\\\"B\\\")\",\n",
" \"msprintf(C, symbol=\\\"C\\\")\"\n",
")\n",
"\n",
"table(\"msprintf(M, symbol=\\\"M\\\", digit=5)\",\n",
" \"msprintf(A, symbol=\\\"A\\\", digit=5)\",\n",
" \"msprintf(B, symbol=\\\"B\\\", digit=5)\",\n",
" \"msprintf(C, symbol=\\\"C\\\", digit=5)\"\n",
")\n",
"\n",
"table(\"msprintf(M, symbol=\\\"M\\\", digit=2)\",\n",
" \"msprintf(A, symbol=\\\"A\\\", digit=2)\",\n",
" \"msprintf(B, symbol=\\\"B\\\", digit=2)\",\n",
" \"msprintf(C, symbol=\\\"C\\\", digit=2)\"\n",
")\n",
"\n",
"table(\"msprintf(M, symbol=\\\"M\\\", digit=0)\",\n",
" \"msprintf(A, symbol=\\\"A\\\", digit=0)\",\n",
" \"msprintf(B, symbol=\\\"B\\\", digit=0)\",\n",
" \"msprintf(C, symbol=\\\"C\\\", digit=0)\"\n",
")\n",
"\n",
"table(\"msprintf(M, symbol=\\\"M\\\", format=\\\"%d\\\")\",\n",
" \"msprintf(A, symbol=\\\"A\\\", format=\\\"%d\\\")\",\n",
" \"msprintf(B, symbol=\\\"B\\\", format=\\\"%d\\\")\",\n",
" \"msprintf(C, symbol=\\\"C\\\", format=\\\"%d\\\")\"\n",
")\n",
"\n",
"table(\"msprintf(M, symbol=\\\"M\\\", format=\\\"%8.1e\\\")\",\n",
" \"msprintf(A, symbol=\\\"A\\\", format=\\\"%8.1e\\\")\",\n",
" \"msprintf(B, symbol=\\\"B\\\", format=\\\"%8.1e\\\")\",\n",
" \"msprintf(C, symbol=\\\"C\\\", format=\\\"%8.1e\\\")\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`mprintf(M)` is same as `println(msprintf(M))`. If you want to get the return value as a string, use `msprintf(M)`. `format=\"\"` is the same as the first argument of `@sprintf` and `@printf`. For more information about `format=\"\"`, see [my article](https://zenn.dev/ohno/articles/8b35354a8140f8) or [the official documentation](https://docs.julialang.org/en/v1/stdlib/Printf/)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.1",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}