{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# \n", "\n", "# Using Julia as a calculator\n", "\n", "### Quick background\n", "\n", "Read about this material here: [Julia as a\n", "calculator](http://mth229.github.io/calculator.html).\n", "\n", "For the impatient, these questions cover the use of `Julia` to replace\n", "what a calculator can do.\n", "\n", "#### Using Jupyter\n", "\n", "This workspace is a [Jupyter](https://jupyter.org/) notebook. For a bit\n", "of orientation:\n", "\n", "- Commands are typed into cells. There can be one or more commands in\n", " a cell. Use a semicolon or newline to separate commands.\n", "- Cells are executed in different ways: using “shift-enter,” using the\n", " “Run” button in the tool bar, or using the “Cell” menu in the menu\n", " bar.\n", "- Output from execution appears below the cell. Output shows the last\n", " command executed\n", "- After a cell is executed, a running tally of what was executed when\n", " is displayed next to the `In [...]` and `Out[...]` labels.\n", "- The actually running of a cell is executed in a kernel which may be\n", " on a different machine.\n", "- Some cells – such as this one – are marked `Markdown` in the\n", " toolbar, this is for formatted text. You will type commands into\n", " cells designated as “Code.”\n", "\n", "#### The project notebooks\n", "\n", "The project notebooks, like this one, offer a brief introduction into a\n", "topic and then allow you to continue within the notebook. There is also\n", "a blank notebook to use without introductory text.\n", "\n", "It is suggested that you use the “Run” menu to “Run all cells” when\n", "these notebook are loaded.\n", "\n", "#### The common operations on numbers: addition, subtraction, multiplication, division, and powers.\n", "\n", "For the most part there is no surprise, once you learn the notations:\n", "`+`, `-`, `*`, `/`, and `^`. (Though you may find that copying and\n", "pasting minus signs will often cause an error, as only something that\n", "looks like a minus sign is pasted in.)\n", "\n", "Using `IJulia`, one types the following into a cell and then presses the\n", "*run* button (or *shift-enter*):" ], "id": "6d0c19e0-8424-4147-85f0-a2a7feb0b4d7" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "4" ] } } ], "source": [ "2 + 2" ], "id": "2" }, { "cell_type": "markdown", "metadata": {}, "source": [ "The answer follows below the cell.\n", "\n", "Here is how one does a slightly more complicated computation:" ], "id": "0bb6c888-f87a-4eb8-b5fe-d03a1dc2d30c" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "56.81818181818182" ] } } ], "source": [ "(2 + 3)^4/(5 + 6)" ], "id": "4" }, { "cell_type": "markdown", "metadata": {}, "source": [ "As with your calculator, it is very important to use parentheses, as\n", "appropriate, to circumvent the usual order of operations.\n", "\n", "One aspect that can be surprising is multiplication by a literal number\n", "does not require a `*` to be typed (e.g. `2x` or `2*x`). However, when\n", "no `*` is given, the order of operation is different than may be\n", "expected.\n", "\n", "#### The use of the basic families of function: trigonometric, exponential, logarithmic.\n", "\n", "On a calculator, there are buttons used to compute various functions. In\n", "`Julia`, there are *many* pre-defined functions that serve a similar\n", "role (In the next project you will see how to define your own).\n", "\n", "Functions in `Julia` have names and are **called** using parentheses to\n", "enclose their argument(s), as with:" ], "id": "98871a32-615d-4cd8-bf02-b176675b8c43" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "(0.7071067811865475, 0.5000000000000001)" ] } } ], "source": [ "sin(pi/4), cos(pi/3)" ], "id": "6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(With `IJulia`, when a cell is executed only the last command computed\n", "is displayed, the above shows that using a comma to separate commands on\n", "the same line can be used to get two or more commands to be displayed.)\n", "\n", "Most basic functions in `Julia` have easy to guess names, though you\n", "will need to learn some differences, such as `log` is for $\\ln$ and\n", "`asin` for $\\sin^{-1}$. Some function names encountered in this class\n", "include: `sqrt`, `cbrt`, `sin`, `cos`, `tan`, `asin`, `acos`,\n", "`atan`,`exp`, and `log`.\n", "\n", "The trigonometric functions use *radians*. For degree measure the\n", "conversion factor $\\pi/180$ must be judiciously employed.\n", "\n", "The function $f(x) = e^x$ is implemented with function `exp(x)` and\n", "**not** using `e^x`. In base Julia `e` is not defined.\n", "\n", "#### The use of memory registers to remember intermediate values.\n", "\n", "Rather than have numbered memory registers like a calculator, it is\n", "*easy* to assign a name to a value. For example," ], "id": "818d6f4d-9e8f-47e6-8575-1284eac09e60" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "42" ] } } ], "source": [ "x = 42" ], "id": "8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Names can be reassigned and repurposed. *However*, a function name can\n", "not be repurposed as a variable name and a variable name can not be\n", "repurposed as a function name. You will get an `ERROR` message if this\n", "is tried. (A suggestion is to use longer descriptive names or follow\n", "math conventions: use `x`, `y`, `z`, … for variables; `f` and `g` for\n", "functions; and `a`,`b`, … for parameters, though even then values like\n", "`h` may play a dual role)\n", "\n", "For assigning more than one value at once, commas can be used as with:" ], "id": "fbc607ba-3829-477e-97fb-9d2ad02e942a" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "(1, 2, 3)" ] } } ], "source": [ "a, b, c = 1, 2, 3" ], "id": "10" }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Julia, like math, has different number types\n", "\n", "Unlike a calculator, but just like math, `Julia` has different types of\n", "numbers: integers, rational numbers, real numbers, and complex numbers.\n", "For the most part the distinction isn’t much to worry about, but there\n", "are times where one must, such as overflow with integers. (One can only\n", "take the factorial of 20 with 64-bit integers, whereas on most\n", "calculators a factorial of 69 can be taken, but not 70.) Julia\n", "automatically assigns a type when it parses a value: a `1` will be an\n", "integer, a `1.0` an floating point number. Rational numbers are made by\n", "using two division symbols, `1//2`.\n", "\n", "For many operations the type will be conserved, such as adding two\n", "integers. For some operations, the type will be converted, such as\n", "dividing two integer values. Mathematically, we know we can divide some\n", "integers and still get an integer, but `Julia` usually opts for the same\n", "output for its functions (and division is also a function) based on the\n", "type of the input, not the specific values of the input.\n", "\n", "## Numbers\n", "\n", "Scientific notation represents real numbers as $\\pm a \\cdot 10^b$, where\n", "$b$ is an integer, and $a$ may be a real number in the range\n", "$[1.0, 10)$. In `Julia` such numbers are represented with an `e` to\n", "replace the 10, as with `1.2e3` which would be $1.2 \\cdot 10^3$ (better\n", "known as 1,230) or `3.2e-1`, which would be $3.2 \\cdot 10^{-1}$ (which\n", "is equal to $0.32$). Take note that this $e$ is not the special base of\n", "the natural logarithm, but is just notation indicating a power of $10$.\n", "To use this notation, you must have a number immediately before the “e”,\n", "not a space or an asterisk. That is, these are **not correct:** `1.2*e3`\n", "or `12 e 3`.\n", "\n", "------------------------------------------------------------------------" ], "id": "c99a87a8-8d4d-4799-b22b-18301238f05a" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Your commands go here\n", "\n" ], "id": "12" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernel_info": { "name": "julia" }, "kernelspec": { "name": "julia", "display_name": "Julia", "language": "julia" }, "language_info": { "name": "julia", "codemirror_mode": "julia", "version": "1.10.0" } } }