Taking the Derivative

Similar documents
Splay Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

Elixir, functional programming and the Lambda calculus.

2.9 Linear Approximations and Differentials

Building a system for symbolic differentiation

An introduction to Scheme

Compilers and computer architecture Code-generation (2): register-machines

AVL Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

Lecture #3: Environments

Building a system for symbolic differentiation

1 Basic Mathematical Operations

6.001 Notes: Section 8.1

Partial Derivatives (Online)

Chapter 1. Fundamentals of Higher Order Programming

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

x 6 + λ 2 x 6 = for the curve y = 1 2 x3 gives f(1, 1 2 ) = λ actually has another solution besides λ = 1 2 = However, the equation λ

7.1 INVERSE FUNCTIONS

Standard Boolean Forms

CS 360 Programming Languages Interpreters

Examples from Section 7.1: Integration by Parts Page 1

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

Chordy: a distributed hash table. Johan Montelius and Vladimir Vlassov. August 29, 2016

LECTURE 0: Introduction and Background

NAME: Section # SSN: X X X X

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Approximate First and Second Derivatives

Properties of Criteria. 1. Applicability Property. Criteria

lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

ENVIRONMENT DIAGRAMS AND RECURSION 2

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

10. Functions (Part 2)

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

SCHEME AND CALCULATOR 5b

1 Maple Introduction. 1.1 Getting Started. 1.2 Maple Commands

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Basic stuff -- assignments, arithmetic and functions

6.001: Structure and Interpretation of Computer Programs

UNIVERSITETET I OSLO

AP CALCULUS BC 2013 SCORING GUIDELINES

Erlang: concurrent programming. Johan Montelius. October 2, 2016

An Erlang primer. Johan Montelius

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Our Strategy for Learning Fortran 90

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, 2018 Solutions. 1 Higher Order Functions. HOFs in Environment Diagrams

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

A Small Web Server. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

It can be confusing when you type something like the expressions below and get an error message. a range variable definition a vector of sine values

Drawing curves automatically: procedures as arguments

Week 5: Geometry and Applications

4 Visualization and. Approximation

RECURSION: n. SEE RECURSION 3

Functional programming in LISP

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

List Functions, and Higher-Order Functions

Objectives. 1 Basic Calculations. 2 Matrix Algebra. Physical Sciences 12a Lab 0 Spring 2016

Using IDLE for

APPM 2460 PLOTTING IN MATLAB

Parallel Programming

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Arithmetic expressions can be typed into Maple using the regular operators:

John Perry. Fall 2013

Symbolic Subdifferentiation in Python

CIS 194: Homework 5. Due Friday, October 3, Rings. No template file is provided for this homework. Download the

CSc 372 Comparative Programming Languages

Symbolic and Automatic Di erentiation in Python

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

1. No calculators or other electronic devices are allowed during this exam.

1.7 Limit of a Function

(Refer Slide Time 6:48)

AP Calculus AB Unit 2 Assessment

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

11 Linear Programming

Programming Lecture 3

Unit #11 : Integration by Parts, Average of a Function. Goals: Learning integration by parts. Computing the average value of a function.

Intro. Speed V Growth

Dr. Allen Back. Nov. 21, 2014

2.2 Order of Operations

Classes of Real Numbers 1/2. The Real Line

2 Second Derivatives. As we have seen, a function f (x, y) of two variables has four different partial derivatives: f xx. f yx. f x y.

Sheet 4. Example 1. a) Expand f(x) = x 2 in a Fourier series on [ π, π]. Use the even-odd idea to reduce your work.

. Tutorial Class V 3-10/10/2012 First Order Partial Derivatives;...

HANDS-ON START TO WOLFRAM MATHEMATICA. and Programming with the Wolfram Language. Cliff Hastings Kelvin Mischo Michael Morrison.

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

( 3) ( 4 ) 1. Exponents and Radicals ( ) ( xy) 1. MATH 102 College Algebra. still holds when m = n, we are led to the result

Functions and Graphs. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996.

John Perry. Spring 2017

MATH 104 Sample problems for first exam - Fall MATH 104 First Midterm Exam - Fall (d) 256 3

Proving Properties of Recursive Functions and Data Structures. CS 270 Math Foundations of CS Jeremy Johnson

LAB 9 The Performance of MIPS

Expressions and Casting

Solution of final examination

6.001 Notes: Section 15.1

, has the form T i1i 2 i m. = κ i1i 2 i m. x i1. 1 xi2 2 xim m (2)

Transcription:

Taking the Derivative Programming II - Elixir Version Johan Montelius Spring Term 2018 Introduction You of course know how to take the derivative of a function, this is something you probably learned already in high school. There are some simple rules that one applies and in five minutes one can find the derivative of even complicated functions. Our task in this tutorial is to work with functions as symbolic expressions and compute the derivative of functions. Part of the problem is to understand how we can represent functions using the data structures that we have in Elixir. Once we know how to do this the derivation will be quite simple. 1 Representing functions You might wonder what the problem is; after all we represent a function in Elixir using the syntax: fn(x) -> x * 2 end This is only partly true, what we do is express what the function should look like, how it is actually represented is hidden to us. If we write: f = fn(x) -> x * 2 end then f is a function of one argument that we can use, but if we only have access to f we can not determine that it is a function of one argument nor ID1019 KTH 1 / 5

that the the body of the function is a multiplication of the argument and a constant. In order to differentiate the function f, with respect to x, we need to know that the body of the expression is x * 2. We need to be able to examine the expressions x*2, and determine that it is a product of the variable x and 2, only then can we determine that the derivative is 2. So, just because we have functions in Elixir, does not mean that we necessarily should use them to represent our functions. 2 An expression Let s start by finding a representation for constants and variables. We can limit ourselves to the domain of reals and a real number could of course be represented by an Elixir number. This is of course trivial but remember to separate the real number 2.34 from the representation in Elixir by 2.34. A variable could be represented as an atom so the variable x is represented by the Elixir atom :x. This simple mapping of elements in our domain to Elixir data structures is tempting to use but it has some disadvantages, we can not use pattern matching to determine if an element is a number or a variable. In our program we would have to use the built-in recognizers to separate the two cases; we would use code as the following: def derivative(n) when is_number(n) do:... def derivative(n) when is_atom(n) do:... We would also have to think twice when we want to include constants such as π, could we represent it using the atom :pi? Moreover, when we derivatives, is it important to separate the constant π from the constant 2.34? A better approach (all though it will turn our expressions into huge data structures) is to be more explicit in our choice of representation. What if we choose to represent all constants using the tuple {:const, c} where c could be either an atom (:pi) or a number (2.34). Variables could, in the same way, be represented by tuples {:var, v} where v is a atom that identifies the variable. If constants and variables are our literals, we have the following definition: ID1019 KTH 2 / 5

@type literal() :: {:const, number()} {:const, atom()} {:var, atom()} 3 Expressions Assume that we, for the time being, limit ourselves to the arithmetic operations multiplication and addition, we have a very natural representation. We will of course represent them using tuples where the first element is an atom that identifies the operation. We have {:mul, a, b} and {:add, a, b}. Expressions are thus: @type expr() :: {:add, expr(), expr()} {:mul, expr(), expr()} literal() This gives us everything we need to represent a limited sets of expressions. The expression 2x + 3 could for example be represented by the Elixir structure: {:add, {:mul, {:const, 2}, {:var, :x}}, {:const, 3}} As you see it it is not a syntax we would like to use when we write expressions by had but it has its advantages when it comes to handle the expressions using Elixir clauses. 4 The derivative of What are the rules of derivation? You of course remember that derivative of 2x + 3 with respect to x is 2, and that the derivative of x 2 is 2x but now we should define a program that does this automatically so we need to have very clear understanding if the rules. If you have not done so yet, this is the point where you should brush up on derivative rules so that you can follow the reasoning. These are two rules that we will use: dx x 1 ID1019 KTH 3 / 5

dxc 0 for any literal different from x dx f(x) + g(x) f (x) + g (x) dx f(x) g(x) f (x) g(x) + f(x) g (x) The third rule is quite straight-forward; the derivative of an sum is the sum of the derivatives of the terms. The derivative of 4x 2 + 2x + 5 is 8x + 2 since since the derivative of 4x 2 is 8x etc. The last rule, you might not even have learned as a rule but simply it s consequences in the most common cases. It is the rule that says that 4x 2 is 8x and 2x is 2. When learning using examples like these the general rule is quite simple: multiply the constant with the power and reduce the power by one. The general rule gives us the definition for any product, be it 2x or 2x 2. 5 The rules of the game So if we know how expressions are represented and how to take the derivative of sums and products, we are ready to implement the rules. This is a skeleton on what a function deriv/2 would look like: def deriv({:const, _}, _), do:... def deriv({:var, v}, v), do:... def deriv({:var, y}, _), do:... def deriv({:mul, e1, e2}, v), do:... def deriv({:add, e1, e2}, v), do:... What is the derivative of 2x 2 +3x+5 with respect to x? How do we represent the expression in our system? Can you calculate the derivative using the deriv/2 function? The answer that you get might not look like the answer you would have hoped for but it might be that what you see is an expression that can be simplified. The derivative of 2x is of course 2 but our function will return something that looks like 0 x + 2 1, which of course is equal to 2. ID1019 KTH 4 / 5

6 Carrying on Add more rules to the deriv/2 function. We should of course be able to take the derivative of the following expressions: x n ln(x) 1 x x sin(x) To be able to handle these expressions you of course need to find a suitable representation. You then have to find the general rule for finding the derivative. 7 Simplification The results of our derivation might be correct but they are very hard to read. They contain multiplications with zero, addition with constant values etc. All of those could be removed if we simplified the results. Simplification could be tricky, You could start by transforming an expression so that all functions with constant arguments were actually evaluated. You could then remove expressions that are multiplied with zero etc. The problem is to know if there are any more things that could be done; how do we know that will not be able to do more. There will also be a discussion of what the simplest form would look like. Should we write x (y + 2) or should we write xy + 2x ID1019 KTH 5 / 5