F# for Scientists. Jon Harrop Flying Frog Consultancy Ltd. Foreword by Don Syme A JOHN WILEY & SONS, INC., PUBLICATION WILEY

Similar documents
F# for Scientists. Jon Harrop Flying Frog Consultancy Ltd. Foreword by Don Syme WILEY A JOHN WILEY & SONS, INC., PUBLICATION

Programming in Scala Second Edition

PROBLEM SOLVING WITH FORTRAN 90

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Part I Basic Concepts 1

Microsoft. Microsoft Visual C# Step by Step. John Sharp

Contents Computing with Formulas

VERIFICATION AND VALIDATION FOR QUALITY OF UML 2.0 MODELS

Chapters are PDF documents posted online at the book s Companion Website (located at

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

Whom Is This Book For?... xxiv How Is This Book Organized?... xxiv Additional Resources... xxvi

WITH C+ + William Ford University of the Pacific. William Topp University of the Pacific. Prentice Hall, Englewood Cliffs, New Jersey 07632

"Charting the Course... Java Programming Language. Course Summary

C# in Depth THIRD EDITION

Chapters and Appendix F are PDF documents posted online at the book s Companion Website (located at

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

object/relational persistence What is persistence? 5

A Web-Based Introduction

F. THOMSON LEIGHTON INTRODUCTION TO PARALLEL ALGORITHMS AND ARCHITECTURES: ARRAYS TREES HYPERCUBES

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

Table of Contents. Preface... xxi

Advanced R. V!aylor & Francis Group. Hadley Wickham. ~ CRC Press

C# 6.0 in a nutshell / Joseph Albahari & Ben Albahari. 6th ed. Beijin [etc.], cop Spis treści

TH IRD EDITION. Python Cookbook. David Beazley and Brian K. Jones. O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Tokyo

Contents. Chapter 1 SPECIFYING SYNTAX 1

Integrated Algebra 2 and Trigonometry. Quarter 1

Python Scripting for Computational Science

"Charting the Course... SharePoint 2007 Hands-On Labs Course Summary

Acknowledgments Introduction. Part I: Programming Access Applications 1. Chapter 1: Overview of Programming for Access 3

Digital Signal Processing with Field Programmable Gate Arrays

Excel Programming with VBA (Macro Programming) 24 hours Getting Started

Microsoft Visual C# Step by Step. John Sharp

Contents. Acknowledgments Parachutes: Coda. About the Author. Presentation Conventions. PART ONE Foundations 1

Object-Oriented Programming and Data Structures

PYTHON. p ykos vtawynivis. Second eciitiovl. CO Ve, WESLEY J. CHUN

COURSE: DATA STRUCTURES USING C & C++ CODE: 05BMCAR17161 CREDITS: 05

Contents. Preface xvii Acknowledgments. CHAPTER 1 Introduction to Parallel Computing 1. CHAPTER 2 Parallel Programming Platforms 11

Summary of Contents LIST OF FIGURES LIST OF TABLES

Excel for Chemists. Second Edition

Python Scripting for Computational Science

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Managing Your Biological Data with Python

Acknowledgments. Chapter 1: Primer in Excel VBA 1. Chapter 2: The Application Object 63

C# in Depth SECOND EDITION JON SKEET. MANNING Greenwich (74 w. long.)

Acknowledgments Introduction. Chapter 1: Introduction to Access 2007 VBA 1. The Visual Basic Editor 18. Testing Phase 24

What s different about Factor?

Chapter 1: Excel in a Nutshell 3 Chapter 2: Basic Facts about Formulas 33 Chapter 3: Working with Names 59

C# Programming: From Problem Analysis to Program Design. Fourth Edition

Data Mining with Microsoft

Data Structures in C++ Using the Standard Template Library

Introduction to Programming Using Java (98-388)

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Introduction. Assessment Test. Part I The Programmer s Exam 1

Programming. Principles and Practice Using C++ Bjarne Stroustrup. / Addison-Wesley. Second Edition

LOGIC AND DISCRETE MATHEMATICS

Mathematics Shape and Space: Polygon Angles

About the Authors. Preface

"Charting the Course... MOC Programming in C# with Microsoft Visual Studio Course Summary

AP Computer Science A Course Syllabus

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

CHAPTER 1: INTRODUCTION TO THE IDE 3

[CHAPTER] 1 INTRODUCTION 1

COPYRIGHTED MATERIAL. Contents. Part I: C# Fundamentals 1. Chapter 1: The.NET Framework 3. Chapter 2: Getting Started with Visual Studio

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

JAVA CONCEPTS Early Objects

List of Figures. About the Authors. Acknowledgments

CHAPTER 1: INTRODUCING C# 3

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

Compiler Construction Using

Microsoft Visual Studio 2010

Structured Parallel Programming Patterns for Efficient Computation

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

Mathematica for Scientists and Engineers

Contents. Preface. About the Authors BASIC TECHNIQUES CHAPTER 1 PARALLEL COMPUTERS. l. 1 The Demand for Computational Speed 3

BFW-037 B.Sc. FOOTWEAR TECHNOLOGY (BSCFWT) Term-End Examination. December, 2014

SOME ASSEMBLY REQUIRED

Pointers in C. A Hands on Approach. Naveen Toppo. Hrishikesh Dewan

Hands-On Introduction to. LabVIEW. for Scientists and Engineers. Second Edition. John Essick. Reed College OXFORD UNIVERSITY PRESS

C o n t e n t S i n D e ta i l FOrewOrd by Matt Graeber xii PreFaCe xvii C# CraSH COurSe FuzzinG and exploiting xss and SQL injection

3.5 Filtering with the 2D Fourier Transform Basic Low Pass and High Pass Filtering using 2D DFT Other Low Pass Filters

Computers as Components Principles of Embedded Computing System Design

Pro ODP.NET for Oracle. Database 11 g. Edmund Zehoo. Apress

About the Author. Dependency Chart. Chapter 1: Logic and Sets 1. Chapter 2: Relations and Functions, Boolean Algebra, and Circuit Design

The Designer's Guide to VHDL Second Edition

Python Training. Complete Practical & Real-time Trainings. A Unit of SequelGate Innovative Technologies Pvt. Ltd.

Digital Image Processing

THE DESIGN AND ANALYSIS OF COMPUTER ALGORITHMS

A First Course in Scientific Computing

Introduction to Algorithms Third Edition

COAL (COmplex Arithmetic Language) Reference Manual

Beginning Groovy, Grails and Griffon. Vishal Layka Christopher M. Judd Joseph Faisal Nusairat Jim Shingler

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Absolute C++ Walter Savitch

PERL 6 and PARROT An Overview. Presentation for NYSA The New York System Administrators Association

Essential MATLAB for Engineers and Scientists

Beginning ASP.NET. 4.5 in C# Matthew MacDonald

Graphics Shaders. Theory and Practice. Second Edition. Mike Bailey. Steve Cunningham. CRC Press. Taylor&FnincIs Croup tootutor London New York

SRE VIDYASAAGAR HIGHER SECONDARY SCHOOL. TWO MARKS

INFORMATION HIDING IN COMMUNICATION NETWORKS

Transcription:

F# for Scientists Jon Harrop Flying Frog Consultancy Ltd. Foreword by Don Syme WILEY A JOHN WILEY & SONS, INC., PUBLICATION

Preface Acknowledgments List of Figi ares List of Tables Acronyms 1 Introduction 1.1 1.2 1.3 1.4 Programming guidelines A brief history of F# Benefits of F# Introducing F# 1.4.1 Language overview 1.4.2 Pattern matching 1.4.3 Equality 1.4.4 Sequence expressions 4 XIX xxi xxiii xxv xxxi ХХХШ 1 2 2 3 3 4 15 24 26 ix

X 1.4.5 Exceptions 27 1.5 Imperative programming 29 1.6 Functional programming 31 1.6.1 Immutability 31 1.6.2 Recursion 32 1.6.3 Curried functions 33 1.6.4 Higher-order functions 35 Program Structure 37 2.,1 Nesting 38 2.2 Factormg 38 2.2.1 Factoring out common subexpressions 39 2.2.2 Factoring out higher-order functions 39 2.3 Modules 42 2.4 Objects 44 2.4.1 Augmentations 44 2.4.2 Classes 46 2.5 Functional design patterns 49 2.5.1 Combinators 49 2.5.2 Maps and folds 52 2.6 F# development 53 2.6.1 Creating an F# project 54 2.6.2 Building executables 54 2.6.3 Debugging 56 2.6.4 Interactive mode 56 2.6.5 C# interoperability 58 Data Structures 63 3.1 Algorithmic complexity 64 3.1.1 3.1.2 Primitive operations Complexity 64 65 3.2 Arrays А ТТ'ЯЛ/'С 69 r\.l I ay a 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 Array literals Array indexing Array concatenation Aliasing Subarrays Creation 69 70 70 71 72 72

XI 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.2.7 3.2.8 3.2.9 3.2.10 3.2.11 Lists 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 Sets 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 Hash tables 3.5.1 3.5.2 3.5.3 3.5.4 Maps 3.6.1 3.6.2 3.6.3 Iteration Map Folds Sorting Pattern matching Sorting Searching Filtering Maps and folds Pattern matching Creation Insertion Cardinality Set-theoretic operations Comparison Creation Searching Insertion, replacement a Higher-order functions Creation Searching Higher-order functions Choosing a data structure Sequen ces Heterogeneous containers Trees 3.10.1 3.10.2 3.10.3 Balanced trees Unbalanced trees Abstract syntax trees 72 73 73 75 75 75 76 76 78 78 80 82 82 83 83 83 84 84 85 86 86 87 87 88 89 90 91 92 92 93 100 101 110 Numerical Analysis 4.1 Number representation 4.1.1 Machine-precision integers 4.1.2 Machine-precision floating-point numbers 113 113 113 114

XII 4.2 Algebra 117 4.3 Interpolation 119 4.4 Quadratic solutions 120 4.5 Mean and variance 122 4.6 Other forms of arithmetic 123 4.6.1 Arbitrary-precision integer arithmetic 123 4.6.2 Arbitrary-precision rational arithmetic 124 4.6.3 Adaptive precision 125 Input and Output 5.1 5.2 5.3 5.4 5.5 Printiflg 5.1.1 Generating strings Generic printing Reading from and writing to files Serialization Lexing and parsing 5.5.1 5.5.2 Lexing Parsing Simple Examples 6.1 6.2 6.3 Functional 6.1.1 Nest 6.1.2 Fixed point 6.1.3 Within 6.1.4 Memoize 6.1.5 Binary search Numerical 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.2.7 6.2.8 6.2.9 Heaviside step Kronecker ^-function Gaussian Binomial coefficients Root finding Grad Function minimization Gamma function Discrete wavelet transform String related 6.3.1 Transcribing DNA 127 127 128 129 130 131 132 133 137 141 141 142 142 142 143 147 147 147 148 148 149 151 151 152 154 155 157 157

6.3.2 Word frequency 6.4 List related 6.4.1 count 6.4.2 positions 6.4.3 fold_to 6.4.4 insert 6.4.5 chop 6.4.6 dice 6.4.7 apply_at 6.4.8 sub 6.4.9 extract 6.4.10 shuffle 6.4.11 transpose 6.4.12 combinations 6.4.13 distribute 6.4.14 permute 6.4.15 Power set 6.5 Array related 6.5.1 rotate 6.5.2 swap 6.5.3 except 6.5.4 shuffle 6.6 Higher-order functions 6.6.1 Tuple related 6.6.2 Generalized products Visualization 7.1 Windows Forms 7.1.1 Forms 7.1.2 Controls 7.1.3 Events 7.1.4 Bitmaps 7.1.5 Example: Cellular automata 7.1.6 Running an application 7.2 Managed DirectX 7.2.1 Handling DirectX devices 7.2.2 Programmatic rendering 7.2.3 Rendering an icosahedron

XIV 7.2.4 Declarative rendering 191 7.2.5 Spawning visualizations from the F# interactive mode 192 7.3 Tesselating objects into triangles 194 7.3.1 Spheres 194 7.3.2 3D function plotting 196 Optimization 8.1 4 8.2 8.3 8.4 Libraries 9.1 9.2 9.3 Timing 8.1.1 8.1.2 8.1.3 % 8.1.4 Absolute time CPU time Looping Example timing Profilin j 8.2.1 8-queens problem Algorithmic optimizations Lower-level optimizations 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.4.6 8.4.7 8.4.8 8.4.9 8.4.10 8.4.11 8.4.12 Benchmarking data structures Compiler flags Tail-recursion Avoiding allocation Terminating early Avoiding higher-order functions Use mutable Specialized functions Unboxing data structures Eliminate needless closures Inlining Serializing Loading I.NET libraries Charting and graphing Threads 9.3.1 Thread safety 9.3.2 Basic use 9.3.3 Locks 9.3.4 The thread pool 9.3.5 Asynchronous delegates 199 200 200 201 201 202 202 202 205 206 207 211 212 213 217 220 220 221 222 223 224 224 225 226 226 227 228 229 231 232 233

XV 9.3.6 Background threads 233 9.4 Random numbers 234 9.5 Regular expressions 234 9.6 Vectors and matrices 235 9.7 Downloading from the Web 236 9.8 Compression 237 9.9 Handling XML 237 9.9.1 Reading 237 9.9.2 Writing 238 9.9.3 Declarative representation 238 9.10 Calling native libraries 239 9.11 Fourier transform 240 9.11.1 Native-code bindings 240 9.11.2 Interface in F# 242 9.11.3 Pretty printing complex numbers 243 9.11.4 Example use 244 9.12 Metaprogramming 245 9.12.1 Emitting IL code 245 9.12.2 Compiling with LINQ 247 10 Databases 249 10.1 Protein data bank 250 10.1.1 Interrogating the PDB 250 10.1.2 Pretty printing XML in F# interactive sessions 251 10.1.3 Deconstructing XML using active patterns 251 10.1.4 Visualization in a GUI 253 10.2 Web services 254 10.2.1 US temperature by zip code 255 10.2.2 Interrogating the NCBI 256 10.3 Relational databases 258 10.3.1 Connection to a database 259 10.3.2 Executing SQL statements 259 10.3.3 Evaluating SQL expressions 261 10.3.4 Interrogating the database programmatically 261 10.3.5 Filling the database from a data structure 263 10.3.6 Visualizing the result 263 10.3.7 Cleaning up 264 ч

XVi 11 Interoperability 267 11.1 Excel 267 11.1.1 Referencing the Excel interface 268 11.1.2 Loading an existing spreadsheet 268 11.1.3 Creating a new spreadsheet 269 11.1.4 Referring to a worksheet 269 11.1.5 Writing cell values into a worksheet 270 11.1.6 Reading cell values from a worksheet 271 11.2 MATLAB 272 11.2.1 Creating a.net interface from a COM interface 272 11.2.2% Using the interface 273 11.2.3 Remote execution of MATLAB commands 273 11.2.4 Reading and writing MATLAB variables 273 11.3 Mathematica 275 11.3.1 Using.NET-link 275 11.3.2 Example 277 12 Complete Examples 281 12.1 12.2 12.3 12.4 12.5 Fast Fourier transform 12.1.1 Discrete Fourier transform 12.1.2 Danielson-Lanczos algorithm 12.1.3 Bluestein's convolution algorithm 12.1.4 Testing and performance Semi-circle law 12.2.1 Eigenvalue computation 12.2.2 Injecting results into Excel 12.2.3 Results Finding n -nearest neighbors 12.3.1 Formulation 12.3.2 Representing an atomic configuration 12.3.3 Parser 12.3.4 Lexer 12.3.5 Main program 12.3.6 Visualization Logistic map Real-time particle dynamics 281 282 283 285 287 288 289 290 291 291 292 295 295 297 297 299 301 303 Appendix A: Troubleshooting 311

XVII АЛ A.2 А.З А.4 A.5 А.6 А.7 А.8 Glossary Value restriction Mutable array contents Negative literals Accidental capture Local and non-local variable definitions Merging lines Applications that do not die Beware of "it" 311 312 313 313 313 314 314 315 317 Bibliography 325 Index 329