{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction notebook\n", "\n", "This page gives a similar introduction as [intro.rst](intro.rst), but written as a ``jupyter`` notebook.\n", "\n", "``exampy`` is an example Python package that contains some very basic math functions. As an example, we can compute the square of a number as" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.0" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import exampy\n", "exampy.square(3.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, we can compute the cube of a number:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "27.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exampy.cube(3.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A general method for raising a number to a given power is given by the Pow class. For example, to get the fourth power of 3, do:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "81.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "po= exampy.Pow(p=4.)\n", "po(3.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``exampy`` also includes a simple method for integrating a function, in the ``exampy.integrate`` submodule. This submodule contains the function ``riemann`` that approximates the integral of any one-parameter function as a Riemann sum. ``riemann`` takes as input (i) the function to integrate, (ii) the integration range’s lower limit and (iii) the upper limit, and (iv) optionally, the number of intervals to divide the integration range in. For example, the integrate the square function of the range [0,1], do:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.35185185185185186" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from exampy import integrate\n", "integrate.riemann(exampy.square,0,1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we increase the number of intervals from the default (which is 10), we get a better approximation to the correct result (which is 1/3):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.33350016683350014" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "integrate.riemann(exampy.square,0,1,n=1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }