1309 lines
98 KiB
HTML
1309 lines
98 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||
|
||
<meta charset="utf-8">
|
||
<meta name="generator" content="quarto-1.0.32">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||
|
||
|
||
<title>Calculus with Julia - 17 Overview of Julia commands</title>
|
||
<style>
|
||
code{white-space: pre-wrap;}
|
||
span.smallcaps{font-variant: small-caps;}
|
||
span.underline{text-decoration: underline;}
|
||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||
ul.task-list{list-style: none;}
|
||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||
.sourceCode { overflow: visible; }
|
||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||
div.sourceCode { margin: 1em 0; }
|
||
pre.sourceCode { margin: 0; }
|
||
@media screen {
|
||
div.sourceCode { overflow: auto; }
|
||
}
|
||
@media print {
|
||
pre > code.sourceCode { white-space: pre-wrap; }
|
||
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||
}
|
||
pre.numberSource code
|
||
{ counter-reset: source-line 0; }
|
||
pre.numberSource code > span
|
||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||
pre.numberSource code > span > a:first-child::before
|
||
{ content: counter(source-line);
|
||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||
border: none; display: inline-block;
|
||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||
-khtml-user-select: none; -moz-user-select: none;
|
||
-ms-user-select: none; user-select: none;
|
||
padding: 0 4px; width: 4em;
|
||
color: #aaaaaa;
|
||
}
|
||
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
||
div.sourceCode
|
||
{ }
|
||
@media screen {
|
||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||
}
|
||
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
||
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
||
code span.at { color: #7d9029; } /* Attribute */
|
||
code span.bn { color: #40a070; } /* BaseN */
|
||
code span.bu { } /* BuiltIn */
|
||
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
||
code span.ch { color: #4070a0; } /* Char */
|
||
code span.cn { color: #880000; } /* Constant */
|
||
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
||
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
||
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
||
code span.dt { color: #902000; } /* DataType */
|
||
code span.dv { color: #40a070; } /* DecVal */
|
||
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
||
code span.ex { } /* Extension */
|
||
code span.fl { color: #40a070; } /* Float */
|
||
code span.fu { color: #06287e; } /* Function */
|
||
code span.im { } /* Import */
|
||
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
||
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
||
code span.op { color: #666666; } /* Operator */
|
||
code span.ot { color: #007020; } /* Other */
|
||
code span.pp { color: #bc7a00; } /* Preprocessor */
|
||
code span.sc { color: #4070a0; } /* SpecialChar */
|
||
code span.ss { color: #bb6688; } /* SpecialString */
|
||
code span.st { color: #4070a0; } /* String */
|
||
code span.va { color: #19177c; } /* Variable */
|
||
code span.vs { color: #4070a0; } /* VerbatimString */
|
||
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
||
</style>
|
||
|
||
|
||
<script src="../site_libs/quarto-nav/quarto-nav.js"></script>
|
||
<script src="../site_libs/quarto-nav/headroom.min.js"></script>
|
||
<script src="../site_libs/clipboard/clipboard.min.js"></script>
|
||
<script src="../site_libs/quarto-search/autocomplete.umd.js"></script>
|
||
<script src="../site_libs/quarto-search/fuse.min.js"></script>
|
||
<script src="../site_libs/quarto-search/quarto-search.js"></script>
|
||
<meta name="quarto:offset" content="../">
|
||
<link href="../limits/limits.html" rel="next">
|
||
<link href="../precalc/trig_functions.html" rel="prev">
|
||
<script src="../site_libs/quarto-html/quarto.js"></script>
|
||
<script src="../site_libs/quarto-html/popper.min.js"></script>
|
||
<script src="../site_libs/quarto-html/tippy.umd.min.js"></script>
|
||
<script src="../site_libs/quarto-html/anchor.min.js"></script>
|
||
<link href="../site_libs/quarto-html/tippy.css" rel="stylesheet">
|
||
<link href="../site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
|
||
<script src="../site_libs/bootstrap/bootstrap.min.js"></script>
|
||
<link href="../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||
<link href="../site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
|
||
<script id="quarto-search-options" type="application/json">{
|
||
"location": "navbar",
|
||
"copy-button": false,
|
||
"collapse-after": 3,
|
||
"panel-placement": "end",
|
||
"type": "overlay",
|
||
"limit": 20,
|
||
"language": {
|
||
"search-no-results-text": "No results",
|
||
"search-matching-documents-text": "matching documents",
|
||
"search-copy-link-title": "Copy link to search",
|
||
"search-hide-matches-text": "Hide additional matches",
|
||
"search-more-match-text": "more match in this document",
|
||
"search-more-matches-text": "more matches in this document",
|
||
"search-clear-button-title": "Clear",
|
||
"search-detached-cancel-button-title": "Cancel",
|
||
"search-submit-button-title": "Submit"
|
||
}
|
||
}</script>
|
||
<script async="" src="https://hypothes.is/embed.js"></script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" integrity="sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==" crossorigin="anonymous"></script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
|
||
<script type="application/javascript">define('jquery', [],function() {return window.jQuery;})</script>
|
||
|
||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
|
||
|
||
</head>
|
||
|
||
<body class="nav-sidebar floating nav-fixed">
|
||
|
||
<div id="quarto-search-results"></div>
|
||
<header id="quarto-header" class="headroom fixed-top">
|
||
<nav class="navbar navbar-expand-lg navbar-dark ">
|
||
<div class="navbar-container container-fluid">
|
||
<a class="navbar-brand" href="../index.html">
|
||
<img src="../logo.png" alt="">
|
||
<span class="navbar-title">Calculus with Julia</span>
|
||
</a>
|
||
<div id="quarto-search" class="" title="Search"></div>
|
||
</div> <!-- /container-fluid -->
|
||
</nav>
|
||
<nav class="quarto-secondary-nav" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
|
||
<div class="container-fluid d-flex justify-content-between">
|
||
<h1 class="quarto-secondary-nav-title"><span class="chapter-number">17</span> <span class="chapter-title">Overview of Julia commands</span></h1>
|
||
<button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation">
|
||
<i class="bi bi-chevron-right"></i>
|
||
</button>
|
||
</div>
|
||
</nav>
|
||
</header>
|
||
<!-- content -->
|
||
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
|
||
<!-- sidebar -->
|
||
<nav id="quarto-sidebar" class="sidebar collapse sidebar-navigation floating overflow-auto">
|
||
<div class="mt-2 flex-shrink-0 align-items-center">
|
||
<div class="sidebar-search">
|
||
<div id="quarto-search" class="" title="Search"></div>
|
||
</div>
|
||
</div>
|
||
<div class="sidebar-menu-container">
|
||
<ul class="list-unstyled mt-1">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../index.html" class="sidebar-item-text sidebar-link">Preface</a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="true">Precalculus Concepts</a>
|
||
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="true">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 show">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/calculator.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">1</span> <span class="chapter-title">From calculator to computer</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/variables.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">2</span> <span class="chapter-title">Variables</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/numbers_types.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">3</span> <span class="chapter-title">Number systems</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/logical_expressions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">4</span> <span class="chapter-title">Inequalities, Logical expressions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">5</span> <span class="chapter-title">Vectors</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/ranges.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">6</span> <span class="chapter-title">Ranges and Sets</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">7</span> <span class="chapter-title">Functions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">8</span> <span class="chapter-title">The Graph of a Function</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/transformations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">9</span> <span class="chapter-title">Function manipulations</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/inversefunctions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">10</span> <span class="chapter-title">The Inverse of a Function</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/polynomial.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">11</span> <span class="chapter-title">Polynomials</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/polynomial_roots.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">12</span> <span class="chapter-title">Roots of a polynomial</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/polynomials_package.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">13</span> <span class="chapter-title">The Polynomials package</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/rational_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">14</span> <span class="chapter-title">Rational functions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/exp_log_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">15</span> <span class="chapter-title">Exponential and logarithmic functions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/trig_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">16</span> <span class="chapter-title">Trigonometric functions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../precalc/julia_overview.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">17</span> <span class="chapter-title">Overview of Julia commands</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">Limits</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-2" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../limits/limits.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">18</span> <span class="chapter-title">Limits</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../limits/limits_extensions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">19</span> <span class="chapter-title">Limits, issues, extensions of the concept</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../limits/continuity.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">20</span> <span class="chapter-title">Continuity</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../limits/intermediate_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">21</span> <span class="chapter-title">Implications of continuity</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">Derivatives</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-3" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">22</span> <span class="chapter-title">Derivatives</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/numeric_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">23</span> <span class="chapter-title">Numeric derivatives</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/symbolic_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">24</span> <span class="chapter-title">Symbolic derivatives</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">25</span> <span class="chapter-title">The mean value theorem for differentiable functions.</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/optimization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">26</span> <span class="chapter-title">Optimization</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/first_second_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">27</span> <span class="chapter-title">The first and second derivatives</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/curve_sketching.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">28</span> <span class="chapter-title">Curve Sketching</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/linearization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">29</span> <span class="chapter-title">Linearization</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/newtons_method.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">30</span> <span class="chapter-title">Newton’s method</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/more_zeros.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">31</span> <span class="chapter-title">Derivative-free alternatives to Newton’s method</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/lhospitals_rule.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">32</span> <span class="chapter-title">L’Hospital’s Rule</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/implicit_differentiation.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">33</span> <span class="chapter-title">Implicit Differentiation</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/related_rates.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">34</span> <span class="chapter-title">Related rates</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../derivatives/taylor_series_polynomials.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">35</span> <span class="chapter-title">Taylor Polynomials and other Approximating Polynomials</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">Integrals</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-4" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">36</span> <span class="chapter-title">Area under a curve</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/ftc.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">37</span> <span class="chapter-title">Fundamental Theorem or Calculus</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/substitution.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">38</span> <span class="chapter-title">Substitution</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/integration_by_parts.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">39</span> <span class="chapter-title">Integration By Parts</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/partial_fractions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">40</span> <span class="chapter-title">Partial Fractions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/improper_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">41</span> <span class="chapter-title">Improper Integrals</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">42</span> <span class="chapter-title">Mean value theorem for integrals</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/area_between_curves.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">43</span> <span class="chapter-title">Area between two curves</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/center_of_mass.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">44</span> <span class="chapter-title">Center of Mass</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/volumes_slice.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">45</span> <span class="chapter-title">Volumes by slicing</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/arc_length.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">46</span> <span class="chapter-title">Arc length</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integrals/surface_area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">47</span> <span class="chapter-title">Surface Area</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">ODEs</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-5" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../ODEs/odes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">48</span> <span class="chapter-title">ODEs</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../ODEs/euler.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">49</span> <span class="chapter-title">Euler’s method</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../ODEs/solve.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">50</span> <span class="chapter-title">The problem-algorithm-solve interface</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../ODEs/differential_equations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">51</span> <span class="chapter-title">The <code>DifferentialEquations</code> suite</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="false">Differential vector calculus</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-6" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../differentiable_vector_calculus/polar_coordinates.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">52</span> <span class="chapter-title">Polar Coordinates and Curves</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../differentiable_vector_calculus/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">53</span> <span class="chapter-title">Vectors and matrices</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../differentiable_vector_calculus/vector_valued_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">54</span> <span class="chapter-title">Vector-valued functions, <span class="math inline">\(f:R \rightarrow R^n\)</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../differentiable_vector_calculus/scalar_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">55</span> <span class="chapter-title">Scalar functions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../differentiable_vector_calculus/scalar_functions_applications.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">56</span> <span class="chapter-title">Applications with scalar functions</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../differentiable_vector_calculus/vector_fields.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">57</span> <span class="chapter-title">Functions <span class="math inline">\(R^n \rightarrow R^m\)</span></span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../differentiable_vector_calculus/plots_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">58</span> <span class="chapter-title">2D and 3D plots in Julia with Plots</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">Integral vector calculus</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-7" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integral_vector_calculus/double_triple_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">59</span> <span class="chapter-title">Multi-dimensional integrals</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integral_vector_calculus/line_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">60</span> <span class="chapter-title">Line and Surface Integrals</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integral_vector_calculus/div_grad_curl.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">61</span> <span class="chapter-title">The Gradient, Divergence, and Curl</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integral_vector_calculus/stokes_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">62</span> <span class="chapter-title">Green’s Theorem, Stokes’ Theorem, and the Divergence Theorem</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../integral_vector_calculus/review.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">63</span> <span class="chapter-title">Quick Review of Vector Calculus</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="false">Alternatives</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-8" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../alternatives/plotly_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">64</span> <span class="chapter-title">JavaScript based plotting libraries</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../alternatives/makie_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">65</span> <span class="chapter-title">Calculus plots with Makie</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item sidebar-item-section">
|
||
<div class="sidebar-item-container">
|
||
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">Appendices</a>
|
||
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">
|
||
<i class="bi bi-chevron-right ms-2"></i>
|
||
</a>
|
||
</div>
|
||
<ul id="quarto-sidebar-section-9" class="collapse list-unstyled sidebar-section depth1 ">
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../misc/getting_started_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">66</span> <span class="chapter-title">Getting started with Julia</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../misc/julia_interfaces.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">67</span> <span class="chapter-title">Julia interfaces</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../misc/calculus_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">68</span> <span class="chapter-title">The <code>CalculusWithJulia</code> package</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../misc/unicode.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">69</span> <span class="chapter-title">Usages of Unicode symbols</span></a>
|
||
</div>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../misc/quick_notes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">70</span> <span class="chapter-title">Quick introduction to Calculus with Julia</span></a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="sidebar-item">
|
||
<div class="sidebar-item-container">
|
||
<a href="../references.html" class="sidebar-item-text sidebar-link">References</a>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
<!-- margin-sidebar -->
|
||
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
|
||
<nav id="TOC" role="doc-toc">
|
||
<h2 id="toc-title">Table of contents</h2>
|
||
|
||
<ul>
|
||
<li><a href="#installing-julia" id="toc-installing-julia" class="nav-link active" data-scroll-target="#installing-julia"> <span class="header-section-number">17.1</span> Installing <code>Julia</code></a></li>
|
||
<li><a href="#interacting-with-julia" id="toc-interacting-with-julia" class="nav-link" data-scroll-target="#interacting-with-julia"> <span class="header-section-number">17.2</span> Interacting with <code>Julia</code></a></li>
|
||
<li><a href="#augmenting-base-julia" id="toc-augmenting-base-julia" class="nav-link" data-scroll-target="#augmenting-base-julia"> <span class="header-section-number">17.3</span> Augmenting base <code>Julia</code></a></li>
|
||
<li><a href="#julia-commands" id="toc-julia-commands" class="nav-link" data-scroll-target="#julia-commands"> <span class="header-section-number">17.4</span> <code>Julia</code> commands</a></li>
|
||
<li><a href="#numbers-variable-types" id="toc-numbers-variable-types" class="nav-link" data-scroll-target="#numbers-variable-types"> <span class="header-section-number">17.5</span> Numbers, variable types</a>
|
||
<ul class="collapse">
|
||
<li><a href="#vectors" id="toc-vectors" class="nav-link" data-scroll-target="#vectors"> <span class="header-section-number">17.5.1</span> Vectors</a></li>
|
||
</ul></li>
|
||
<li><a href="#variables" id="toc-variables" class="nav-link" data-scroll-target="#variables"> <span class="header-section-number">17.6</span> Variables</a></li>
|
||
<li><a href="#functions" id="toc-functions" class="nav-link" data-scroll-target="#functions"> <span class="header-section-number">17.7</span> Functions</a>
|
||
<ul class="collapse">
|
||
<li><a href="#built-in-functions" id="toc-built-in-functions" class="nav-link" data-scroll-target="#built-in-functions"> <span class="header-section-number">17.7.1</span> Built-in functions</a></li>
|
||
<li><a href="#user-defined-functions" id="toc-user-defined-functions" class="nav-link" data-scroll-target="#user-defined-functions"> <span class="header-section-number">17.7.2</span> User-defined functions</a></li>
|
||
</ul></li>
|
||
<li><a href="#conditional-statements" id="toc-conditional-statements" class="nav-link" data-scroll-target="#conditional-statements"> <span class="header-section-number">17.8</span> Conditional statements</a></li>
|
||
<li><a href="#looping" id="toc-looping" class="nav-link" data-scroll-target="#looping"> <span class="header-section-number">17.9</span> Looping</a></li>
|
||
<li><a href="#broadcasting-mapping" id="toc-broadcasting-mapping" class="nav-link" data-scroll-target="#broadcasting-mapping"> <span class="header-section-number">17.10</span> Broadcasting, mapping</a></li>
|
||
<li><a href="#plotting" id="toc-plotting" class="nav-link" data-scroll-target="#plotting"> <span class="header-section-number">17.11</span> Plotting</a></li>
|
||
<li><a href="#equations" id="toc-equations" class="nav-link" data-scroll-target="#equations"> <span class="header-section-number">17.12</span> Equations</a></li>
|
||
<li><a href="#symbolic-math" id="toc-symbolic-math" class="nav-link" data-scroll-target="#symbolic-math"> <span class="header-section-number">17.13</span> Symbolic math</a></li>
|
||
</ul>
|
||
<div class="toc-actions"><div><i class="bi bi-github"></i></div><div class="action-links"><p><a href="https://github.com/jverzani/CalculusWithJuliaNotes.jl/edit/main/quarto/precalc/julia_overview.qmd" class="toc-action">Edit this page</a></p><p><a href="https://github.com/jverzani/CalculusWithJuliaNotes.jl/issues/new" class="toc-action">Report an issue</a></p></div></div></nav>
|
||
</div>
|
||
<!-- main -->
|
||
<main class="content" id="quarto-document-content">
|
||
|
||
<header id="title-block-header" class="quarto-title-block default">
|
||
<div class="quarto-title">
|
||
<h1 class="title d-none d-lg-block"><span class="chapter-number">17</span> <span class="chapter-title">Overview of Julia commands</span></h1>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="quarto-title-meta">
|
||
|
||
|
||
|
||
</div>
|
||
|
||
|
||
</header>
|
||
|
||
<p>The <a href="http://www.julialang.org"><code>Julia</code></a> programming language is well suited as a computer accompaniment while learning the concepts of calculus. The following overview covers the language-specific aspects of the pre-calculus part of the <a href="calculuswithjulia.github.io">Calculus with Julia</a> notes.</p>
|
||
<section id="installing-julia" class="level2" data-number="17.1">
|
||
<h2 data-number="17.1" class="anchored" data-anchor-id="installing-julia"><span class="header-section-number">17.1</span> Installing <code>Julia</code></h2>
|
||
<p><code>Julia</code> is an <em>open source</em> project which allows anyone with a supported computer to use it. To install locally, the <a href="https://julialang.org/downloads/">downloads</a> page has several different binaries for installation. Additionally, the downloads page contains a link to a docker image. For Microsoft Windows, the new <a href="https://github.com/JuliaLang/juliaup">juilaup</a> installer may be of interest; it is available from the Windows Store. <code>Julia</code> can also be compiled from source.</p>
|
||
<p><code>Julia</code> can also be run through the web. The <a href="https://mybinder.org/">https://mybinder.org/</a> service in particular allows free access, though limited in terms of allotted memory and with a relatively short timeout for inactivity.</p>
|
||
<p><a href="https://mybinder.org/v2/gh/CalculusWithJulia/CwJScratchPad.git/master">Launch Binder</a></p>
|
||
</section>
|
||
<section id="interacting-with-julia" class="level2" data-number="17.2">
|
||
<h2 data-number="17.2" class="anchored" data-anchor-id="interacting-with-julia"><span class="header-section-number">17.2</span> Interacting with <code>Julia</code></h2>
|
||
<p>At a basic level, <code>Julia</code> provides a means to read commands or instructions, evaluate those commands, and then print or return those commands. At a user level, there are many different ways to interact with the reading and printing. For example:</p>
|
||
<ul>
|
||
<li>The REPL. The <code>Julia</code> terminal is the built-in means to interact with <code>Julia</code>. A <code>Julia</code> Terminal has a command prompt, after which commands are typed and then sent to be evaluated by the <code>enter</code> key. The terminal may look something like the following where <code>2+2</code> is evaluated:</li>
|
||
</ul>
|
||
<hr>
|
||
<pre class="{verbatim}"><code>$ julia
|
||
_
|
||
_ _ _(_)_ | Documentation: https://docs.julialang.org
|
||
(_) | (_) (_) |
|
||
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
|
||
| | | | | | |/ _` | |
|
||
| | |_| | | | (_| | | Version 1.7.0 (2021-11-30)
|
||
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|
||
|__/ |
|
||
|
||
julia> 2 + 2
|
||
4</code></pre>
|
||
<hr>
|
||
<ul>
|
||
<li>An IDE. For programmers, an integrated development environment is often used to manage bigger projects. <code>Julia</code> has <code>Juno</code> and <code>VSCode</code>.</li>
|
||
<li>A notebook. The <a href="https://jupyter.org/">Project Juptyer</a> provides a notebook interface for interacting with <code>Julia</code> and a more <code>IDE</code> style <code>jupyterlab</code> interface. A jupyter notebook has cells where commands are typed and immediately following is the printed output returned by <code>Julia</code>. The output of a cell depends on the state of the kernel when the cell is computed, not the order of the cells in the notebook. Cells have a number attached, showing the execution order. The <code>Juypter</code> notebook is used by <code>binder</code> and can be used locally through the <code>IJulia</code> package. This notebook has the ability to display many different types of outputs in addition to plain text, such as images, marked up math text, etc.</li>
|
||
<li>The <a href="https://github.com/fonsp/Pluto.jl">Pluto</a> package provides a <em>reactive</em> notebook interface. Reactive means when one “cell” is modified and executed, the new values cascade to all other dependent cells which in turn are updated. This is very useful for exploring a parameter space, say. Pluto notebooks can be exported as HTML files which make them easy to read online and – by clever design – embed the <code>.jl</code> file that can run through <code>Pluto</code> if it is downloaded.</li>
|
||
</ul>
|
||
<p>The <code>Pluto</code> interface has some idiosyncracies that need explanation:</p>
|
||
<ul>
|
||
<li>Cells can only have one command within them. Multiple-command cells must be contained in a <code>begin</code> block or a <code>let</code> block.</li>
|
||
<li>By default, the cells are <em>reactive</em>. This means when a variable in one cell is changed, then any references to that variable are also updated – like a spreadsheet. This is fantastic for updating several computations at once. However it means variable names can not be repeated within a page. Pedagogically, it is convenient to use variable names and function names (e.g., <code>x</code> and <code>f</code>) repeatedly, but this is only possible <em>if</em> they are within a <code>let</code> block or a function body.</li>
|
||
<li>To not repeat names, but to be able to reference a value from cell-to-cell, some Unicode variants are used within a page. Visually these look familiar, but typing the names requires some understanding of Unicode input. The primary usages is <em>bold italic</em> (e.g., <code>\bix[tab]</code> or <code>\bif[tab]</code>) or <em>bold face</em> (e.g. <code>\bfx[tab]</code> or <code>bff[tab]</code>).</li>
|
||
<li>The notebooks snapshot the packages they depend on, which is great for reproducability, but may mean older versions are silently used.</li>
|
||
</ul>
|
||
</section>
|
||
<section id="augmenting-base-julia" class="level2" data-number="17.3">
|
||
<h2 data-number="17.3" class="anchored" data-anchor-id="augmenting-base-julia"><span class="header-section-number">17.3</span> Augmenting base <code>Julia</code></h2>
|
||
<p>The base <code>Julia</code> installation has many features, but leaves many others to <code>Julia</code>’s package ecosystem. These notes use packages to provide plotting, symbolic math, access to special functions, numeric routines, and more.</p>
|
||
<p>Within <code>Pluto</code>, using add-on packages is very simple, as <code>Pluto</code> downloads and installs packages when they are requested through a <code>using</code> or <code>import</code> directive.</p>
|
||
<hr>
|
||
<p>For other interfaces to <code>Julia</code> some more detail is needed.</p>
|
||
<p>The <code>Julia</code> package manager makes add-on packages very easy to install.</p>
|
||
<p>Julia comes with just a few built-in packages, one being <code>Pkg</code> which manages subsequent package installation. To add more packages, we first must <em>load</em> the <code>Pkg</code> package. This is done by issuing the following command:</p>
|
||
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">Pkg</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<p>The <code>using</code> command loads the specified package and makes all its <em>exported</em> values available for direct use. There is also the <code>import</code> command which allows the user to select which values should be imported from the package, if any, and otherwise gives access to the new functionality through the dot syntax.</p>
|
||
<p>Packages need to be loaded just once per session.</p>
|
||
<p>To use <code>Pkg</code> to “add” another package, we would have a command like:</p>
|
||
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"CalculusWithJulia"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<p>This command instructs <code>Julia</code> to look at its <em>general registry</em> for the <code>CalculusWithJulia.jl</code> package, download it, then install it. Once installed, a package only needs to be brought into play with the <code>using</code> or <code>import</code> commands.</p>
|
||
<div class="callout-note callout callout-style-default callout-captioned">
|
||
<div class="callout-header d-flex align-content-center">
|
||
<div class="callout-icon-container">
|
||
<i class="callout-icon"></i>
|
||
</div>
|
||
<div class="callout-caption-container flex-fill">
|
||
Note
|
||
</div>
|
||
</div>
|
||
<div class="callout-body-container callout-body">
|
||
<p>In a terminal setting, there is a package mode, entered by typing <code>]</code> as the leading character and exited by entering <code><delete></code> at a blank line. This mode allows direct access to <code>Pkg</code> with a simpler syntax. The command above would be just <code>add CalculusWithJulia</code>.)</p>
|
||
</div>
|
||
</div>
|
||
<p>Packages can be updated through the command <code>Pkg.up()</code>, and removed with <code>Pkg.rm(pkgname)</code>.</p>
|
||
<p>By default packages are installed in a common area. It may be desirable to keep packages for projects isolated. For this the <code>Pkg.activate</code> command can be used. This feature allows a means to have reproducible environments even if <code>Julia</code> or the packages used are upgraded, possibly introducing incompatabilities.</p>
|
||
<p>For these notes, the following packages, among others, are used:</p>
|
||
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"CalculusWithJulia"</span>) <span class="co"># for some simplifying functions and a few packages (SpecialFunctions, ForwardDiff)</span></span>
|
||
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"Plots"</span>) <span class="co"># for basic plotting</span></span>
|
||
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"SymPy"</span>) <span class="co"># for symbolic math</span></span>
|
||
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"Roots"</span>) <span class="co"># for solving `f(x)=0`</span></span>
|
||
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"QuadGk"</span>) <span class="co"># for integration</span></span>
|
||
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"HQuadrature"</span>) <span class="co"># for higher-dimensional integration</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
</section>
|
||
<section id="julia-commands" class="level2" data-number="17.4">
|
||
<h2 data-number="17.4" class="anchored" data-anchor-id="julia-commands"><span class="header-section-number">17.4</span> <code>Julia</code> commands</h2>
|
||
<p>In a <code>Jupyter</code> notebook or <code>Pluto</code> notebook, commands are typed into a notebook cell:</p>
|
||
<div class="cell" data-execution_count="6">
|
||
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fl">2</span> <span class="op">+</span> <span class="fl">2</span> <span class="co"># use shift-enter to evaluate</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="5">
|
||
<pre><code>4</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Commands are executed by using <code>shift-enter</code> or a run button near the cell.</p>
|
||
<p>In <code>Jupyter</code> multiple commands per cell are allowed. In <code>Pluto</code>, a <code>begin</code> or <code>let</code> block is used to collect multiple commmands into a single call. Commands may be separated by new lines or semicolons.</p>
|
||
<p>On a given line, anything <strong>after</strong> a <code>#</code> is a <em>comment</em> and is not processed.</p>
|
||
<p>The results of the last command executed will be displayed in an output area. Separating values by commas allows more than one value to be displayed. Plots are displayed when the plot object is returned by the last executed command.</p>
|
||
<p>In <code>Jupyter</code>, the state of the notebook is a determined by the cells executed along with their order. The state of a <code>Pluto</code> notebook is a result of all the cells in the notebook being executed. The cell order does not impact this and can be rearranged by the user.</p>
|
||
</section>
|
||
<section id="numbers-variable-types" class="level2" data-number="17.5">
|
||
<h2 data-number="17.5" class="anchored" data-anchor-id="numbers-variable-types"><span class="header-section-number">17.5</span> Numbers, variable types</h2>
|
||
<p><code>Julia</code> has many different number types beyond the floating point type employed by most calculators. These include</p>
|
||
<ul>
|
||
<li>Floating point numbers: <code>0.5</code></li>
|
||
<li>Integers: <code>2</code></li>
|
||
<li>Rational numbers: <code>1//2</code></li>
|
||
<li>Complex numbers <code>2 + 0im</code></li>
|
||
</ul>
|
||
<p><code>Julia</code>’s parser finds the appropriate type for the value, when read in. The following all create the number <span class="math inline">\(1\)</span> first as an integer, then a rational, then a floating point number, again as floating point number, and finally as a complex number:</p>
|
||
<div class="cell" data-execution_count="7">
|
||
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fl">1</span>, <span class="fl">1</span><span class="op">//</span><span class="fl">1</span>, <span class="fl">1.0</span>, <span class="fl">1e0</span>, <span class="fl">1</span> <span class="op">+</span> <span class="fl">0im</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="6">
|
||
<pre><code>(1, 1//1, 1.0, 1.0, 1 + 0im)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>As much as possible, operations involving certain types of numbers will produce output of a given type. For example, both of these divisions produce a floating point answer, even though mathematically, they need not:</p>
|
||
<div class="cell" data-execution_count="8">
|
||
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fl">2</span><span class="op">/</span><span class="fl">1</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">2</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="7">
|
||
<pre><code>(2.0, 0.5)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Some powers with negative bases, like <code>(-3.0)^(1/3)</code>, are not defined. However, <code>Julia</code> provides the special-case function <code>cbrt</code> (and <code>sqrt</code>) for handling these.</p>
|
||
<p>Integer operations may silently overflow, producing odd answers, at first glance:</p>
|
||
<div class="cell" data-execution_count="9">
|
||
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fl">2</span><span class="op">^</span><span class="fl">64</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="8">
|
||
<pre><code>0</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>(Though the output is predictable, if overflow is taken into consideration appropriately.)</p>
|
||
<p>When different types of numbers are mixed, <code>Julia</code> will usually promote the values to a common type before the operation:</p>
|
||
<div class="cell" data-execution_count="10">
|
||
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>(<span class="fl">2</span> <span class="op">+</span> <span class="fl">1</span><span class="op">//</span><span class="fl">2</span>) <span class="op">+</span> <span class="fl">0.5</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="9">
|
||
<pre><code>3.0</code></pre>
|
||
</div>
|
||
</div>
|
||
<p><code>Julia</code> will first add <code>2</code> and <code>1//2</code> promoting <code>2</code> to rational before doing so. Then add the result, <code>5//2</code> to <code>0.5</code> by promoting <code>5//2</code> to the floating point number <code>2.5</code> before proceeding.</p>
|
||
<p><code>Julia</code> uses a special type to store a handful of irrational constants such as <code>pi</code>. The special type allows these constants to be treated without round off, until they mix with other floating point numbers. There are some functions that require these be explicitly promoted to floating point. This can be done by calling <code>float</code>.</p>
|
||
<p>The standard mathematical operations are implemented by <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>^</code>. Parentheses are used for grouping.</p>
|
||
<section id="vectors" class="level3" data-number="17.5.1">
|
||
<h3 data-number="17.5.1" class="anchored" data-anchor-id="vectors"><span class="header-section-number">17.5.1</span> Vectors</h3>
|
||
<p>A vector is an indexed collection of similarly typed values. Vectors can be constructed with square brackets (syntax for concatenation):</p>
|
||
<div class="cell" data-execution_count="11">
|
||
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">1</span>, <span class="fl">1</span>, <span class="fl">2</span>, <span class="fl">3</span>, <span class="fl">5</span>, <span class="fl">8</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="10">
|
||
<pre><code>6-element Vector{Int64}:
|
||
1
|
||
1
|
||
2
|
||
3
|
||
5
|
||
8</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Values will be promoted to a common type (or type <code>Any</code> if none exists). For example, this vector will have type <code>Float64</code> due to the <code>1/3</code> computation:</p>
|
||
<div class="cell" data-execution_count="12">
|
||
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">1</span>, <span class="fl">1</span><span class="op">//</span><span class="fl">2</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">3</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="11">
|
||
<pre><code>3-element Vector{Float64}:
|
||
1.0
|
||
0.5
|
||
0.3333333333333333</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>(Vectors are used as a return type from some functions, as such, some familiarity is needed.)</p>
|
||
<p>Regular arithmetic sequences can be defined by either:</p>
|
||
<ul>
|
||
<li>Range operations: <code>a:h:b</code> or <code>a:b</code> which produces a generator of values starting at <code>a</code> separated by <code>h</code> (<code>h</code> is <code>1</code> in the last form) until they reach <code>b</code>.</li>
|
||
<li>The <code>range</code> function: <code>range(a, b, length=n)</code> which produces a generator of <code>n</code> values between <code>a</code> and <code>b</code>;</li>
|
||
</ul>
|
||
<p>These constructs return range objects. A range object <em>compactly</em> stores the values it references. To see all the values, they can be collected with the <code>collect</code> function, though this is rarely needed in practice.</p>
|
||
<p>Random sequences are formed by <code>rand</code>, among others:</p>
|
||
<div class="cell" data-execution_count="13">
|
||
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rand</span>(<span class="fl">3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="12">
|
||
<pre><code>3-element Vector{Float64}:
|
||
0.10423678334027975
|
||
0.3613002059686551
|
||
0.13804328154133638</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>The call <code>rand()</code> returns a single random number (in <span class="math inline">\([0,1)\)</span>.)</p>
|
||
</section>
|
||
</section>
|
||
<section id="variables" class="level2" data-number="17.6">
|
||
<h2 data-number="17.6" class="anchored" data-anchor-id="variables"><span class="header-section-number">17.6</span> Variables</h2>
|
||
<p>Values can be assigned variable names, with <code>=</code>. There are some variants</p>
|
||
<div class="cell" data-execution_count="14">
|
||
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>u <span class="op">=</span> <span class="fl">2</span></span>
|
||
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>a_really_long_name <span class="op">=</span> <span class="fl">3</span></span>
|
||
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>a0, b0 <span class="op">=</span> <span class="fl">1</span>, <span class="fl">2</span> <span class="co"># multiple assignment</span></span>
|
||
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a>a1 <span class="op">=</span> a2 <span class="op">=</span> <span class="fl">0</span> <span class="co"># chained assignment, sets a2 and a1 to 0</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="13">
|
||
<pre><code>0</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>The names can be short, as above, or more verbose. Variable names can’t start with a number, but can include numbers. Variables can also include <a href="../misc/unicode.html">Unicode</a> or even be an emoji.</p>
|
||
<div class="cell" data-execution_count="15">
|
||
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>α, β <span class="op">=</span> <span class="cn">π</span><span class="op">/</span><span class="fl">3</span>, <span class="cn">π</span><span class="op">/</span><span class="fl">4</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="14">
|
||
<pre><code>(1.0471975511965976, 0.7853981633974483)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>We can then use the variables to reference the values:</p>
|
||
<div class="cell" data-execution_count="16">
|
||
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>u <span class="op">+</span> a_really_long_name <span class="op">+</span> a0 <span class="op">-</span> b0 <span class="op">+</span> α</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="15">
|
||
<pre><code>5.047197551196597</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Within <code>Pluto</code>, names are idiosyncratic: within the global scope, only a single usage is possible per notebook; functions and variables can be freely renamed; structures can be redefined or renamed; …</p>
|
||
<p>Outside of <code>Pluto</code>, names may be repurposed, even with values of different types (<code>Julia</code> is a dynamic language), save for (generic) function names, which have some special rules and can only be redefined as another function. Generic functions are central to <code>Julia</code>’s design. Generic functions use a method table to dispatch on, so once a name is assigned to a generic function, it can not be used as a variable name; the reverse is also true.</p>
|
||
</section>
|
||
<section id="functions" class="level2" data-number="17.7">
|
||
<h2 data-number="17.7" class="anchored" data-anchor-id="functions"><span class="header-section-number">17.7</span> Functions</h2>
|
||
<p>Functions in <code>Julia</code> are first-class objects. In these notes, we often pass them as arguments to other functions. There are many built-in functions and it is easy to define new functions.</p>
|
||
<p>We “call” a function by passing argument(s) to it, grouped by parentheses:</p>
|
||
<div class="cell" data-execution_count="17">
|
||
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sqrt</span>(<span class="fl">10</span>)</span>
|
||
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sin</span>(<span class="cn">pi</span><span class="op">/</span><span class="fl">3</span>)</span>
|
||
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a><span class="fu">log</span>(<span class="fl">5</span>, <span class="fl">100</span>) <span class="co"># log base 5 of 100</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="16">
|
||
<pre><code>2.8613531161467867</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>With out parentheses, the name (usually) refers to a generic name and the output lists the number of available implementations (methods).</p>
|
||
<div class="cell" data-execution_count="18">
|
||
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a>log</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="17">
|
||
<pre><code>log (generic function with 42 methods)</code></pre>
|
||
</div>
|
||
</div>
|
||
<section id="built-in-functions" class="level3" data-number="17.7.1">
|
||
<h3 data-number="17.7.1" class="anchored" data-anchor-id="built-in-functions"><span class="header-section-number">17.7.1</span> Built-in functions</h3>
|
||
<p><code>Julia</code> has numerous built-in <a href="http://julia.readthedocs.io/">mathematical</a> functions, we review a few here:</p>
|
||
<section id="powers-logs-and-roots" class="level4">
|
||
<h4 class="anchored" data-anchor-id="powers-logs-and-roots">Powers logs and roots</h4>
|
||
<p>Besides <code>^</code>, there are <code>sqrt</code> and <code>cbrt</code> for powers. In addition basic functions for exponential and logarithmic functions:</p>
|
||
<pre class="{verbatim}"><code>sqrt, cbrt
|
||
exp
|
||
log # base e
|
||
log10, log2, # also log(b, x)</code></pre>
|
||
</section>
|
||
<section id="trigonometric-functions" class="level4">
|
||
<h4 class="anchored" data-anchor-id="trigonometric-functions">Trigonometric functions</h4>
|
||
<p>The <code>6</code> standard trig functions are implemented; their implementation for degree arguments; their inverse functions; and the hyperbolic analogs.</p>
|
||
<pre class="{verbatim}"><code>sin, cos, tan, csc, sec, cot
|
||
asin, acos, atan, acsc, asec, acot
|
||
sinh, cosh, tanh, csch, sech, coth
|
||
asinh, acosh, atanh, acsch, asech, acoth</code></pre>
|
||
<p>If degrees are preferred, the following are defined to work with arguments in degrees:</p>
|
||
<pre class="{verbatim}"><code>sind, cosd, tand, cscd, secd, cotd</code></pre>
|
||
</section>
|
||
<section id="useful-functions" class="level4">
|
||
<h4 class="anchored" data-anchor-id="useful-functions">Useful functions</h4>
|
||
<p>Other useful and familiar functions are defined:</p>
|
||
<ul>
|
||
<li><code>abs</code>: absolute value</li>
|
||
<li><code>sign</code>: is <span class="math inline">\(\lvert x \rvert/x\)</span> except at <span class="math inline">\(x=0\)</span>, where it is <span class="math inline">\(0\)</span>.</li>
|
||
<li><code>floor</code>, <code>ceil</code>: greatest integer less or least integer greater</li>
|
||
<li><code>max(a,b)</code>, <code>min(a,b)</code>: larger (or smaller) of <code>a</code> or <code>b</code></li>
|
||
<li><code>maximum(xs)</code>, <code>minimum(xs)</code>: largest or smallest of the collection referred to by <code>xs</code></li>
|
||
</ul>
|
||
<hr>
|
||
<p>In a Pluto session, the “Live docs” area shows inline documentation for the current object.</p>
|
||
<p>For other uses of <code>Julia</code>, the built-in documentation for an object is accessible through a leading <code>?</code>, say, <code>?sign</code>. There is also the <code>@doc</code> macro, for example:</p>
|
||
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@doc</span> sign</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<hr>
|
||
</section>
|
||
</section>
|
||
<section id="user-defined-functions" class="level3" data-number="17.7.2">
|
||
<h3 data-number="17.7.2" class="anchored" data-anchor-id="user-defined-functions"><span class="header-section-number">17.7.2</span> User-defined functions</h3>
|
||
<p>Simple mathematical functions can be defined using standard mathematical notation:</p>
|
||
<div class="cell" data-execution_count="20">
|
||
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="op">-</span><span class="fl">16</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fl">100</span>x <span class="op">+</span> <span class="fl">2</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="18">
|
||
<pre><code>f (generic function with 1 method)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>The argument <code>x</code> is passed into the body of function.</p>
|
||
<p>Other values are found from the environment where defined:</p>
|
||
<div class="cell" data-hold="true" data-execution_count="21">
|
||
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>a <span class="op">=</span> <span class="fl">1</span></span>
|
||
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fl">2</span><span class="op">*</span>a <span class="op">+</span> x</span>
|
||
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(<span class="fl">3</span>) <span class="co"># 2 * 1 + 3</span></span>
|
||
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a>a <span class="op">=</span> <span class="fl">4</span></span>
|
||
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(<span class="fl">3</span>) <span class="co"># now 2 * 4 + 3</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="19">
|
||
<pre><code>11</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>User-defined functions can have <span class="math inline">\(0\)</span>, <span class="math inline">\(1\)</span> or more arguments:</p>
|
||
<div class="cell" data-execution_count="22">
|
||
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">area</span>(w, h) <span class="op">=</span> w<span class="op">*</span>h</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="20">
|
||
<pre><code>area (generic function with 1 method)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Julia makes different <em>methods</em> for <em>generic</em> function names, so function definitions whose argument specification is different are for different uses, even if the name is the same. This is <em>polymorphism</em>. The practical use is that it means users need only remember a much smaller set of function names, as attempts are made to give common expectations to the same name. (That is, <code>+</code> should be used only for “add” ing objects, however defined.)</p>
|
||
<p>Functions can be defined with <em>keyword</em> arguments that may have defaults specified:</p>
|
||
<div class="cell" data-hold="true" data-execution_count="23">
|
||
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x; m<span class="op">=</span><span class="fl">1</span>, b<span class="op">=</span><span class="fl">0</span>) <span class="op">=</span> m<span class="op">*</span>x <span class="op">+</span> b <span class="co"># note ";"</span></span>
|
||
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(<span class="fl">1</span>) <span class="co"># uses m=1, b=0 -> 1 * 1 + 0</span></span>
|
||
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(<span class="fl">1</span>, m<span class="op">=</span><span class="fl">10</span>) <span class="co"># uses m=10, b=0 -> 10 * 1 + 0</span></span>
|
||
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(<span class="fl">1</span>, m<span class="op">=</span><span class="fl">10</span>, b<span class="op">=</span><span class="fl">5</span>) <span class="co"># uses m=10, b=5 -> 10 * 1 + 5</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="21">
|
||
<pre><code>15</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Longer functions can be defined using the <code>function</code> keyword, the last command executed is returned:</p>
|
||
<div class="cell" data-execution_count="24">
|
||
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">𝒇</span>(x)</span>
|
||
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> y <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span></span>
|
||
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a> z <span class="op">=</span> y <span class="op">-</span> <span class="fl">3</span></span>
|
||
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a> z</span>
|
||
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="22">
|
||
<pre><code>𝒇 (generic function with 1 method)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Functions without names, <em>anonymous functions</em>, are made with the <code>-></code> syntax as in:</p>
|
||
<div class="cell" data-execution_count="25">
|
||
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>x <span class="op">-></span> <span class="fu">cos</span>(x)<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fu">cos</span>(<span class="fl">2</span>x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="23">
|
||
<pre><code>#13 (generic function with 1 method)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>These are useful when passing a function to another function or when writing a function that <em>returns</em> a function.</p>
|
||
</section>
|
||
</section>
|
||
<section id="conditional-statements" class="level2" data-number="17.8">
|
||
<h2 data-number="17.8" class="anchored" data-anchor-id="conditional-statements"><span class="header-section-number">17.8</span> Conditional statements</h2>
|
||
<p><code>Julia</code> provides the traditional <code>if-else-end</code> statements, but more conveniently has a <code>ternary</code> operator for the simplest case:</p>
|
||
<div class="cell" data-execution_count="26">
|
||
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="fu">our_abs</span>(x) <span class="op">=</span> (x <span class="op"><</span> <span class="fl">0</span>) ? <span class="op">-</span>x <span class="op">:</span> x</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="24">
|
||
<pre><code>our_abs (generic function with 1 method)</code></pre>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="looping" class="level2" data-number="17.9">
|
||
<h2 data-number="17.9" class="anchored" data-anchor-id="looping"><span class="header-section-number">17.9</span> Looping</h2>
|
||
<p>Iterating over a collection can be done with the traditional <code>for</code> loop. However, there are list comprehensions to mimic the definition of a set:</p>
|
||
<div class="cell" data-execution_count="27">
|
||
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a>[x<span class="op">^</span><span class="fl">2</span> for x <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="25">
|
||
<pre><code>10-element Vector{Int64}:
|
||
1
|
||
4
|
||
9
|
||
16
|
||
25
|
||
36
|
||
49
|
||
64
|
||
81
|
||
100</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>Comprehensions can be filtered through the <code>if</code> keyword</p>
|
||
<div class="cell" data-execution_count="28">
|
||
<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a>[x<span class="op">^</span><span class="fl">2</span> for x <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> if <span class="fu">iseven</span>(x)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="26">
|
||
<pre><code>5-element Vector{Int64}:
|
||
4
|
||
16
|
||
36
|
||
64
|
||
100</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>This is more efficient than creating the collection then filtering, as is done with:</p>
|
||
<div class="cell" data-execution_count="29">
|
||
<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="fu">filter</span>(iseven, [x<span class="op">^</span><span class="fl">2</span> for x <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="27">
|
||
<pre><code>5-element Vector{Int64}:
|
||
4
|
||
16
|
||
36
|
||
64
|
||
100</code></pre>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="broadcasting-mapping" class="level2" data-number="17.10">
|
||
<h2 data-number="17.10" class="anchored" data-anchor-id="broadcasting-mapping"><span class="header-section-number">17.10</span> Broadcasting, mapping</h2>
|
||
<p>A function can be applied to each element of a vector through mapping or broadcasting. The latter is implemented in a succinct notation. Calling a function with a “.” before its opening “(` will apply the function to each individual value in the argument:</p>
|
||
<div class="cell" data-execution_count="30">
|
||
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>,<span class="fl">4</span>,<span class="fl">5</span>]</span>
|
||
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sin</span>.(xs) <span class="co"># gives back [sin(1), sin(2), sin(3), sin(4), sin(5)]</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="28">
|
||
<pre><code>5-element Vector{Float64}:
|
||
0.8414709848078965
|
||
0.9092974268256817
|
||
0.1411200080598672
|
||
-0.7568024953079282
|
||
-0.9589242746631385</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>For “infix” operators, the dot precedes the operator, as in this example instructing pointwise multiplication of each element in <code>xs</code>:</p>
|
||
<pre class="{juila}"><code>xs .* xs</code></pre>
|
||
<p>Alternatively, the more traditional <code>map</code> can be used:</p>
|
||
<div class="cell" data-execution_count="31">
|
||
<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span>(sin, xs)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="29">
|
||
<pre><code>5-element Vector{Float64}:
|
||
0.8414709848078965
|
||
0.9092974268256817
|
||
0.1411200080598672
|
||
-0.7568024953079282
|
||
-0.9589242746631385</code></pre>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="plotting" class="level2" data-number="17.11">
|
||
<h2 data-number="17.11" class="anchored" data-anchor-id="plotting"><span class="header-section-number">17.11</span> Plotting</h2>
|
||
<p>Plotting is <em>not</em> built-in to <code>Julia</code>, rather added through add-on packages. <code>Julia</code>’s <code>Plots</code> package is an interface to several plotting packages. We mention <code>plotly</code> (built-in) for web based graphics, <code>pyplot</code>, and <code>gr</code> (also built into <code>Plots</code>) for other graphics.</p>
|
||
<p>We must load <code>Plots</code> before we can plot (and it must be installed before we can load it):</p>
|
||
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">Plots</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<p>With <code>Plots</code> loaded, we can plot a function by passing the function object by name to <code>plot</code>, specifying the range of <code>x</code> values to show, as follows:</p>
|
||
<div class="cell" data-execution_count="33">
|
||
<div class="sourceCode cell-code" id="cb61"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(sin, <span class="fl">0</span>, <span class="fl">2</span>pi) <span class="co"># plot a function - by name - over an interval [a,b]</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="31">
|
||
<p><img src="julia_overview_files/figure-html/cell-34-output-1.svg" class="img-fluid"></p>
|
||
</div>
|
||
</div>
|
||
<p>!!1 note This is in the form of <strong>the</strong> basic pattern employed: <code>verb(function_object, arguments...)</code>. The verb in this example is <code>plot</code>, the object <code>sin</code>, the arguments <code>0, 2pi</code> to specify <code>[a,b]</code> domain to plot over.</p>
|
||
<p>Plotting more than one function over <code>[a,b]</code> is achieved through the <code>plot!</code> function, which modifies the existing plot (<code>plot</code> creates a new one) by adding a new layer:</p>
|
||
<div class="cell" data-execution_count="34">
|
||
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(sin, <span class="fl">0</span>, <span class="fl">2</span>pi)</span>
|
||
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(cos, <span class="fl">0</span>, <span class="fl">2</span>pi)</span>
|
||
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(zero, <span class="fl">0</span>, <span class="fl">2</span>pi) <span class="co"># add the line y=0</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="32">
|
||
<p><img src="julia_overview_files/figure-html/cell-35-output-1.svg" class="img-fluid"></p>
|
||
</div>
|
||
</div>
|
||
<p>Individual points are added with <code>scatter</code> or <code>scatter!</code>:</p>
|
||
<div class="cell" data-execution_count="35">
|
||
<div class="sourceCode cell-code" id="cb63"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(sin, <span class="fl">0</span>, <span class="fl">2</span>pi, legend<span class="op">=</span><span class="cn">false</span>)</span>
|
||
<span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(cos, <span class="fl">0</span>, <span class="fl">2</span>pi)</span>
|
||
<span id="cb63-3"><a href="#cb63-3" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter!</span>([<span class="cn">pi</span><span class="op">/</span><span class="fl">4</span>, <span class="cn">pi</span><span class="op">+</span><span class="cn">pi</span><span class="op">/</span><span class="fl">4</span>], [<span class="fu">sin</span>(<span class="cn">pi</span><span class="op">/</span><span class="fl">4</span>), <span class="fu">sin</span>(<span class="cn">pi</span> <span class="op">+</span> <span class="cn">pi</span><span class="op">/</span><span class="fl">4</span>)])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="33">
|
||
<p><img src="julia_overview_files/figure-html/cell-36-output-1.svg" class="img-fluid"></p>
|
||
</div>
|
||
</div>
|
||
<p>(The extra argument <code>legend=false</code> suppresses the automatic legend drawing. There are many other useful arguments to adjust a graphic. For example, passing <code>markersize=10</code> to the <code>scatter!</code> command would draw the points larger than the default.)</p>
|
||
<p>Plotting an <em>anonymous</em> function is a bit more immediate than the two-step approach of defining a named function then calling <code>plot</code> with this as an argument:</p>
|
||
<div class="cell" data-execution_count="36">
|
||
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>( x <span class="op">-></span> <span class="fu">exp</span>(<span class="op">-</span>x<span class="op">/</span><span class="cn">pi</span>) <span class="op">*</span> <span class="fu">sin</span>(x), <span class="fl">0</span>, <span class="fl">2</span>pi)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="34">
|
||
<p><img src="julia_overview_files/figure-html/cell-37-output-1.svg" class="img-fluid"></p>
|
||
</div>
|
||
</div>
|
||
<p>The <code>scatter!</code> function used above takes two vectors of values to describe the points to plot, one for the <span class="math inline">\(x\)</span> values and one for the matching <span class="math inline">\(y\)</span> values. The <code>plot</code> function can also produce plots with this interface. For example, here we use a comprehension to produce <code>y</code> values from the specified <code>x</code> values:</p>
|
||
<div class="cell" data-hold="true" data-execution_count="37">
|
||
<div class="sourceCode cell-code" id="cb65"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">251</span>)</span>
|
||
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> [<span class="fu">sin</span>(<span class="fl">2</span>x) <span class="op">+</span> <span class="fu">sin</span>(<span class="fl">3</span>x) <span class="op">+</span> <span class="fu">sin</span>(<span class="fl">4</span>x) for x <span class="kw">in</span> xs]</span>
|
||
<span id="cb65-3"><a href="#cb65-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(xs, ys)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="35">
|
||
<p><img src="julia_overview_files/figure-html/cell-38-output-1.svg" class="img-fluid"></p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="equations" class="level2" data-number="17.12">
|
||
<h2 data-number="17.12" class="anchored" data-anchor-id="equations"><span class="header-section-number">17.12</span> Equations</h2>
|
||
<p>Notation for <code>Julia</code> and math is <em>similar</em> for functions - but not for equations. In math, an equation might look like:</p>
|
||
<p><span class="math display">\[
|
||
x^2 + y^2 = 3
|
||
\]</span></p>
|
||
<p>In <code>Julia</code> the equals sign is <strong>only</strong> for <em>assignment</em> and <em>mutation</em>. The <em>left-hand</em> side of an equals sign in <code>Julia</code> is reserved for a) variable assignment; b) function definition (via <code>f(x) = ...</code>); c) indexed mutation of a vector or array; d) mutation of fields in a structure. (Vectors are indexed by a number allowing retrieval and mutation of the stored value in the container. The notation mentioned here would be <code>xs[2] = 3</code> to mutate the 2nd element of <code>xs</code> to the value <code>3</code>.</p>
|
||
</section>
|
||
<section id="symbolic-math" class="level2" data-number="17.13">
|
||
<h2 data-number="17.13" class="anchored" data-anchor-id="symbolic-math"><span class="header-section-number">17.13</span> Symbolic math</h2>
|
||
<p>Symbolic math is available through an add-on package <code>SymPy</code> (among others). Once loaded, symbolic variables are created with the macro <code>@syms</code>:</p>
|
||
<div class="sourceCode cell-code" id="cb66"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">SymPy</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell" data-execution_count="39">
|
||
<div class="sourceCode cell-code" id="cb67"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x a b c</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="37">
|
||
<pre><code>(x, a, b, c)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>(A macro rewrites values into other commands before they are interpreted. Macros are prefixed with the <code>@</code> sign. In this use, the “macro” <code>@syms</code> translates <code>x a b c</code> into a command involving <code>SymPy</code>s <code>symbols</code> function.)</p>
|
||
<p>Symbolic expressions - unlike numeric expressions - are not immediately evaluated, though they may be simplified:</p>
|
||
<div class="cell" data-execution_count="40">
|
||
<div class="sourceCode cell-code" id="cb69"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a>p <span class="op">=</span> a<span class="op">*</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> b<span class="op">*</span>x <span class="op">+</span> c</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="38">
|
||
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
||
\[
|
||
a x^{2} + b x + c
|
||
\]
|
||
</span>
|
||
</div>
|
||
</div>
|
||
<p>To substitute a value, we can use <code>Julia</code>’s <code>pair</code> notation (<code>variable=>value</code>):</p>
|
||
<div class="cell" data-execution_count="41">
|
||
<div class="sourceCode cell-code" id="cb70"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a><span class="fu">p</span>(x<span class="op">=></span><span class="fl">2</span>), <span class="fu">p</span>(x<span class="op">=></span><span class="fl">2</span>, a<span class="op">=></span><span class="fl">3</span>, b<span class="op">=></span><span class="fl">4</span>, c<span class="op">=></span><span class="fl">1</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="39">
|
||
<pre><code>(4*a + 2*b + c, 21)</code></pre>
|
||
</div>
|
||
</div>
|
||
<p>This is convenient notation for calling the <code>subs</code> function for <code>SymPy</code>.</p>
|
||
<p>SymPy expressions of a single free variable can be plotted directly:</p>
|
||
<div class="cell" data-execution_count="42">
|
||
<div class="sourceCode cell-code" id="cb72"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fl">64</span> <span class="op">-</span> (<span class="fl">1</span><span class="op">/</span><span class="fl">2</span>)<span class="op">*</span><span class="fl">32</span> <span class="op">*</span> x<span class="op">^</span><span class="fl">2</span>, <span class="fl">0</span>, <span class="fl">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||
<div class="cell-output cell-output-display" data-execution_count="40">
|
||
<p><img src="julia_overview_files/figure-html/cell-43-output-1.svg" class="img-fluid"></p>
|
||
</div>
|
||
</div>
|
||
<ul>
|
||
<li>SymPy has functions for manipulating expressions: <code>simplify</code>, <code>expand</code>, <code>together</code>, <code>factor</code>, <code>cancel</code>, <code>apart</code>, <span class="math inline">\(...\)</span></li>
|
||
<li>SymPy has functions for basic math: <code>factor</code>, <code>roots</code>, <code>solve</code>, <code>solveset</code>, <span class="math inline">\(\dots\)</span></li>
|
||
<li>SymPy has functions for calculus: <code>limit</code>, <code>diff</code>, <code>integrate</code>, <span class="math inline">\(\dots\)</span></li>
|
||
</ul>
|
||
|
||
|
||
</section>
|
||
|
||
</main> <!-- /main -->
|
||
<script id="quarto-html-after-body" type="application/javascript">
|
||
window.document.addEventListener("DOMContentLoaded", function (event) {
|
||
const toggleBodyColorMode = (bsSheetEl) => {
|
||
const mode = bsSheetEl.getAttribute("data-mode");
|
||
const bodyEl = window.document.querySelector("body");
|
||
if (mode === "dark") {
|
||
bodyEl.classList.add("quarto-dark");
|
||
bodyEl.classList.remove("quarto-light");
|
||
} else {
|
||
bodyEl.classList.add("quarto-light");
|
||
bodyEl.classList.remove("quarto-dark");
|
||
}
|
||
}
|
||
const toggleBodyColorPrimary = () => {
|
||
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
|
||
if (bsSheetEl) {
|
||
toggleBodyColorMode(bsSheetEl);
|
||
}
|
||
}
|
||
toggleBodyColorPrimary();
|
||
const icon = "";
|
||
const anchorJS = new window.AnchorJS();
|
||
anchorJS.options = {
|
||
placement: 'right',
|
||
icon: icon
|
||
};
|
||
anchorJS.add('.anchored');
|
||
const clipboard = new window.ClipboardJS('.code-copy-button', {
|
||
target: function(trigger) {
|
||
return trigger.previousElementSibling;
|
||
}
|
||
});
|
||
clipboard.on('success', function(e) {
|
||
// button target
|
||
const button = e.trigger;
|
||
// don't keep focus
|
||
button.blur();
|
||
// flash "checked"
|
||
button.classList.add('code-copy-button-checked');
|
||
var currentTitle = button.getAttribute("title");
|
||
button.setAttribute("title", "Copied!");
|
||
setTimeout(function() {
|
||
button.setAttribute("title", currentTitle);
|
||
button.classList.remove('code-copy-button-checked');
|
||
}, 1000);
|
||
// clear code selection
|
||
e.clearSelection();
|
||
});
|
||
function tippyHover(el, contentFn) {
|
||
const config = {
|
||
allowHTML: true,
|
||
content: contentFn,
|
||
maxWidth: 500,
|
||
delay: 100,
|
||
arrow: false,
|
||
appendTo: function(el) {
|
||
return el.parentElement;
|
||
},
|
||
interactive: true,
|
||
interactiveBorder: 10,
|
||
theme: 'quarto',
|
||
placement: 'bottom-start'
|
||
};
|
||
window.tippy(el, config);
|
||
}
|
||
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
|
||
for (var i=0; i<noterefs.length; i++) {
|
||
const ref = noterefs[i];
|
||
tippyHover(ref, function() {
|
||
let href = ref.getAttribute('href');
|
||
try { href = new URL(href).hash; } catch {}
|
||
const id = href.replace(/^#\/?/, "");
|
||
const note = window.document.getElementById(id);
|
||
return note.innerHTML;
|
||
});
|
||
}
|
||
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
|
||
for (var i=0; i<bibliorefs.length; i++) {
|
||
const ref = bibliorefs[i];
|
||
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
|
||
tippyHover(ref, function() {
|
||
var popup = window.document.createElement('div');
|
||
cites.forEach(function(cite) {
|
||
var citeDiv = window.document.createElement('div');
|
||
citeDiv.classList.add('hanging-indent');
|
||
citeDiv.classList.add('csl-entry');
|
||
var biblioDiv = window.document.getElementById('ref-' + cite);
|
||
if (biblioDiv) {
|
||
citeDiv.innerHTML = biblioDiv.innerHTML;
|
||
}
|
||
popup.appendChild(citeDiv);
|
||
});
|
||
return popup.innerHTML;
|
||
});
|
||
}
|
||
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
|
||
var filterRegex = new RegExp('/' + window.location.host + '/');
|
||
var isInternal = (href) => {
|
||
return filterRegex.test(href) || localhostRegex.test(href);
|
||
}
|
||
// Inspect non-navigation links and adorn them if external
|
||
var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external)');
|
||
for (var i=0; i<links.length; i++) {
|
||
const link = links[i];
|
||
if (!isInternal(link.href)) {
|
||
// target, if specified
|
||
link.setAttribute("target", "_blank");
|
||
}
|
||
}
|
||
});
|
||
</script>
|
||
<nav class="page-navigation">
|
||
<div class="nav-page nav-page-previous">
|
||
<a href="../precalc/trig_functions.html" class="pagination-link">
|
||
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">16</span> <span class="chapter-title">Trigonometric functions</span></span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-page nav-page-next">
|
||
<a href="../limits/limits.html" class="pagination-link">
|
||
<span class="nav-page-text"><span class="chapter-number">18</span> <span class="chapter-title">Limits</span></span> <i class="bi bi-arrow-right-short"></i>
|
||
</a>
|
||
</div>
|
||
</nav>
|
||
</div> <!-- /content -->
|
||
<footer class="footer">
|
||
<div class="nav-footer">
|
||
<div class="nav-footer-center">Copyright 2022, John Verzani</div>
|
||
</div>
|
||
</footer>
|
||
|
||
|
||
|
||
</body></html> |