Automating the Tedious Stuff (Functional programming and other Mathematica magic)

Similar documents
Functional Languages. Hwansoo Han

Lambda Calculus. Gunnar Gotshalks LC-1

Chapter 11 :: Functional Languages

MATH Iris Loeb.

Programming Language Pragmatics

Lecture 13: Expression Evaluation

Mathematical Experiments with Mathematica

Lambda Calculus LC-1

Functional Programming. Big Picture. Design of Programming Languages

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

A Theory of Parallel Computation The π-calculus

Introduction. chapter Functions

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Lambda Calculus and Computation

4/19/2018. Chapter 11 :: Functional Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Chapter 5: The Untyped Lambda Calculus

Chapter 1. Fundamentals of Higher Order Programming

Functional Programming

Denotational semantics

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

SD314 Outils pour le Big Data

Introduction to Scheme

CS 11 Haskell track: lecture 1

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

The Haskell HOP: Higher-order Programming

CSE 505: Concepts of Programming Languages

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

LECTURE 17. Expressions and Assignment

Chapter 6 Control Flow. June 9, 2015

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

Intro. Scheme Basics. scm> 5 5. scm>

CMSC 330: Organization of Programming Languages

CS 360 Programming Languages Interpreters

CITS2401 Computer Analysis & Visualisation

INTRODUCTION TO MATHEMATICA FOR PHYSICS 103

Stop coding Pascal. Saturday, April 6, 13

Imperative, OO and Functional Languages A C program is

Problem One: A Quick Algebra Review

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE 341: Programming Languages

Lambda Calculus.

Higher Order Functions in Haskell

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Ph3 Mathematica Homework: Week 1

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Shell CSCE 314 TAMU. Haskell Functions

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Control Flow February 9, Lecture 7

Functional Programming. Pure Functional Languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

LECTURE 16. Functional Programming

Urmas Tamm Tartu 2013

Functional Programming Lecture 1: Introduction

Documentation for LISP in BASIC

(Refer Slide Time: 4:00)

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Power Editing: Hoxie Ackerman. April 1, Building a Productive Editing Environment for Yourself

Denotational Semantics. Domain Theory

Matlab Introduction. Scalar Variables and Arithmetic Operators

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

MATH 162 Calculus II Computer Laboratory Topic: Introduction to Mathematica & Parametrizations

Three-address code (TAC) TDT4205 Lecture 16

Functional Programming Languages (FPL)

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

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

Pattern Matching and Abstract Data Types

Elixir, functional programming and the Lambda calculus.

Introduction to the λ-calculus

Which of the following is not true of FORTRAN?

Untyped Lambda Calculus

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

CSE Theory of Computing Fall 2017 Project 4-Combinator Project

Lecture 5: The Halting Problem. Michael Beeson

CS 6110 S14 Lecture 1 Introduction 24 January 2014

259 Lecture 25: Simple Programming

#23: Sequences March 28, 2009

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 1. ÚVOD DO PŘEDMĚTU, LAMBDA CALCULUS

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

1.3. Conditional expressions To express case distinctions like

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

Programming Systems in Artificial Intelligence Functional Programming

An Introduction to Scheme

p x i 1 i n x, y, z = 2 x 3 y 5 z

CMSC 330: Organization of Programming Languages

Untyped Lambda Calculus

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Transcription:

/22 Automating the Tedious Stuff (Functional programming and other Mathematica magic) Connor Glosser Michigan State University Departments of Physics & Electrical/Computer Engineering π, 2014

/22 Table of Contents 1 Introduction Formalism 2 Functional Programming Background Pure functions & Modules Higher-order functions 3 Patterns, Rules, & Attributes 4 Closing Further resources

/22 Mathematica is great...

/22... but it s also kind of stupid.

5/22 About this talk What this talk is An outline of more idiomatic ways to use Mathematica A sample of ways to use those idioms in research-like contexts Bi-directional!

/22 My #1 Mathematica tip Reset button for the current Mathematica session; completely removes all variables and definitions Sure, you could just run the Remove["Global *"] cell, but buttons are more fun convenient.

/22 A little bit of syntactic sugar

/22 A little bit of syntactic sugar Generally, we write math with infix notation Mathematica also offers prefix and postfix operators for single-argument functions: Cuts down on tedious bracket-matching, but beware associativity and operator precedence!

/22 A little bit of syntactic sugar @ right-associates and has a high precedence: // left-associates and has a low precedence:

/22 Table of Contents 1 Introduction Formalism 2 Functional Programming Background Pure functions & Modules Higher-order functions 3 Patterns, Rules, & Attributes 4 Closing Further resources

/22 History 1936: Alan Turing Alan Turing invents every programming language that will ever be but is shanghaied by British Intelligence to be 007 before he can patent them.

/22 History 1936: Alan Turing Alan Turing invents every programming language that will ever be but is shanghaied by British Intelligence to be 007 before he can patent them. 1936: Alonzo Church Alonzo Church also invents every language that will ever be but does it better. His lambda-calculus is ignored because it is insufficiently C-like. This criticism occurs in spite of the fact that C has not yet been invented. James Iry

0/22 What is functional programming? Programs as functions from inputs to outputs Higher-order functions Functions become a sort of datatype Avoids mutability/state (!!!!) Mathematical by construction (category theory, formal computation) What things are vs. what things do. Lots of list manipulation

1/22 Pure functions No side-effects: functions depend only on inputs f = Function [ x, x + 3]

1/22 Pure functions No side-effects: functions depend only on inputs f = Function [ x, x + 3] Alternatively, g = # + 3&;

1/22 Pure functions No side-effects: functions depend only on inputs f = Function [ x, x + 3] Alternatively, g = # + 3&; Multiple arguments: In [1] := h = #1 + 2*#2&; h[3, 4] Out [1] := 11

1/22 Pure functions No side-effects: functions depend only on inputs f = Function [ x, x + 3] Alternatively, g = # + 3&; Multiple arguments: In [1] := h = #1 + 2*#2&; h[3, 4] Out [1] := 11 Use Block, With, or Module to localize variables in more complicated function structures

2/22 Transforming Data Consider applying a simple (pure!) function to a set of data...... naïvely, with a for-loop: For [i = 1, i < Length [ input ], i++, output [[i]] = Sin [ input [[i]]], ]

2/22 Transforming Data Consider applying a simple (pure!) function to a set of data...... naïvely, with a for-loop: For [i = 1, i < Length [ input ], i++, output [[i]] = Sin [ input [[i]]], ]... with a Table command: output = Table [ Sin [ input [[i]]], {i,1,n}] (like a list comprehension in python!)

2/22 Transforming Data Consider applying a simple (pure!) function to a set of data...... naïvely, with a for-loop: For [i = 1, i < Length [ input ], i++, output [[i]] = Sin [ input [[i]]], ]... with a Table command: output = Table [ Sin [ input [[i]]], {i,1,n}]... with a Map: output = Map [ Sin, input ] (like a list comprehension in python!)

2/22 Transforming Data Consider applying a simple (pure!) function to a set of data...... naïvely, with a for-loop: For [i = 1, i < Length [ input ], i++, output [[i]] = Sin [ input [[i]]], ]... with a Table command: output = Table [ Sin [ input [[i]]], {i,1,n}]... with a Map: output = Map [ Sin, input ] (like a list comprehension in python!)... by cheating with the Listable attribute: output = Sin [ input ]

3/22 Higher-order Functions: Map Map applies a function to each element of a collection without modifying the original. In [1] := Map [f,{1,2,3,x,y,z}] Out [1] := {f[1],f[2],f[3],f[x],f[y],f[z]} Automatically handles length Easily parallelized with ParallelMap Common enough to warrant special syntax: In [2] := f/@{1,2,3,x,y,z} Out [2] := {f[1],f[2],f[3],f[x],f[y],f[z]}

4/22 Higher-order functions: Apply Apply turns a list of things into formal arguments of a function it essentially strips off a set of {}. Similar to Map, transforms a list: In [1] := Apply [f, {1, 2, 3, a, b, c}] Out [1] := f[1, 2, 3, a, b, c] Can operate on levels 1 (default = 0, use @@@ for level 1) In [2] := Apply [f, {{1},{2},{3}}, {1}] Out [2] := {f[1], f[2], f [3]} (* level 1*) Plus & Subtract become really useful wtih Apply 1 # of indices required to specify element

5/22 Higher-order functions: Nest & NestList Nest repeatedly applies a function to an expression NestList does the same, producing a list of the intermediate results Captures iteration as a recursive application of functions In [1] := Nest [f, x, 3] Out [1] := f[f[f[x ]]] Conclusion While Map, Apply, & Nest are all built-in functions, none rely on ideas exclusive to Mathematica; as functional constructs, they very naturally capture specific types of problems & ideas.

6/22 Table of Contents 1 Introduction Formalism 2 Functional Programming Background Pure functions & Modules Higher-order functions 3 Patterns, Rules, & Attributes 4 Closing Further resources

7/22 Patterns What is a pattern? Patterns represent classes of expressions which can be used to automatically simplify or restructure expressions. For example, f[_] and f[x_] both represent the pattern of a function named f with anything as its argument, but f[x_] gives the name x to the argument (whatever it is). Common patterns: x_: anything (with the anything given the name x) x_integer: any integer (given the name x) x_ˆn_: anything to any explicit power (guess their names) f[r_,r_]: a function with two identical arguments and so on

8/22 The Replacement Idiom /. applies a rule or list of rules in an attempt to transform each subpart of an expression In [1] := {x, xˆ2, y, z} /. x -> a Out [1] := {a, aˆ2, y, z} The rule can make use of Mathematica s pattern-matching capabilities: In [2] := 1 + xˆ2 + xˆ4 /. xˆp_ -> f[p] Out [2] := 1 + f [2] + f [4] Useful for structuring solvers: f = x /. DSolve [x [t] == x[t], x, t ][[1]]

9/22 Attributes Attributes let you define general properties of functions, without necessarily giving explicit values. The Listable attribute automatically threads a function over lists that appear as arguments. In [1] := SetAttributes [ f, Listable ] f[{1,2,3}, x] Out [1] := {f[1,x], f[2,x], f[3,x]} Flat, Orderless used to define things like associativity & commutativity (a+b == b+a for the purposes of pattern matching)

0/22 Table of Contents 1 Introduction Formalism 2 Functional Programming Background Pure functions & Modules Higher-order functions 3 Patterns, Rules, & Attributes 4 Closing Further resources

1/22 Some final thoughts 1 Functional programming and pattern matching are both hard and obtuse (at first), but they can be a very elegant way of attacking problems Also good for parallel programing! 2 The best method usually requires a bit of trial-and-error. Experiment! 3 Further resources: The Mathematica documentation is excellent The Wolfram Blog frequently has cool examples in a variety of subjects Essential Mathematica for Students of Science has lots of detailed notebooks for scientific applicaitons Power Programming with Mathematica: antequated, but good

2/22 Figure: https://www.msu.edu/ glosser1/works.html Thanks for listening!