Presentation is loading. Please wait.

Presentation is loading. Please wait.

F# FOR SCIENTIFIC COMPUTING VITALII ZAKHAROV. QUICK INTRODUCTION TO F# F# uses an open development and engineering process. The language evolution process.

Similar presentations


Presentation on theme: "F# FOR SCIENTIFIC COMPUTING VITALII ZAKHAROV. QUICK INTRODUCTION TO F# F# uses an open development and engineering process. The language evolution process."— Presentation transcript:

1 F# FOR SCIENTIFIC COMPUTING VITALII ZAKHAROV

2 QUICK INTRODUCTION TO F# F# uses an open development and engineering process. The language evolution process is managed by Don Syme from Microsoft Research as the BDFL(Benevolent dictator for life) for the language design in conjunction with the F# Software Foundation. Earlier versions of the F# language were designed by Microsoft and Microsoft Research using a closed development process.

3 QUICK INTRODUCTION TO F#: CONTINUATION F# originates from Microsoft Research, Cambridge, and the language was originally designed and implemented by Don Syme. Andrew Kennedy contributed to the design of units of measure. The Visual F# Tools for Visual Studio are developed by Microsoft. The F# Software Foundation developed the F# open- source compiler and tools, incorporating the open-source compiler implementation provided by the Microsoft Visual F# Tools team.

4 WHAT MOTIVATED DON SYME AND HIS TEAM AT MICROSOFT RESEARCH TO PURSUE F# DEVELOPMENT The F# research project sought to create a variant of the OCaml language running on top of the.Net platform. A related project at MSR Cambridge, SML.NET, did the same for Standard ML. (OCaml and Standard ML are both variants of the ML language.) The motivations for this might have included: Showing it can be done. Publishing scientific papers showing it can be done. Because ML is incredibly cool.

5 DIVE INTO F# FUNCTIONAL PROGRAMMING F# is a strongly typed functional-first language that uses type inference. The programmer does not need to declare types—the compiler deduces types during compilation. F# also allows explicit type annotations, and requires them in some situations. F# is an expression-based language using eager evaluation. Every statement in F#, including if expressions, try expressions and loops, is a composable expression with a static type. Functions and expressions that do not return any value have a return type of unit. F# uses the let keyword for binding values to a name. For example: binds the value 7 to the name x.

6 DIVE INTO F#: CONTINUATION FUNCTIONAL PROGRAMMING New types are defined using the type keyword. For functional programming, F# provides tuple, record, discriminated union, list and option types. A tuple represents a collection of n values, where n ≥ 0. The value n is called the arity of the tuple. A 3-tuple would be represented as (A, B, C), where A, B and C are values of possibly different types. A tuple can be used to store values only when the number of values is known at design-time and stays constant throughout execution. A record is a type where the data members are named, as in { Name:string; Age:int }. Records can be created as { Name="AB"; Age=42 }. The with keyword is used to create a copy of a record, as in { r with Name="CD" }, which creates a new record by copying r and changing the value of the Name field (assuming the record created in the last example was named r). A discriminated union type is a type-safe version of C unions. For example,

7 DIVE INTO F#: CONTINUATION FUNCTIONAL PROGRAMMING Values of the union type can correspond to either union case. The types of the values carried by each union case is included in the definition of each case. The list type is an immutable linked list represented either using a head::tail notation (:: is the cons operator) or a shorthand as [item1; item2; item3]. An empty list is written []. The option type is a discriminated union type with choices Some(x) or None. F# types may be generic, implemented as generic.NET types. F# supports lambda functions and closures. All functions in F# are first class values and are immutable. Functions can be curried. Being first-class values, functions can be passed as arguments to other functions. Like other functional programming languages, F# allows function composition using the >> and << operators.

8 DIVE INTO F#: CONTINUATION FUNCTIONAL PROGRAMMING F# provides sequence expressions that define a sequence seq {... }, list [... ] or array [|... |] through code that generates values. For example, forms a sequence of squares of numbers from 0 to 14 by filtering out numbers from the range of numbers from 0 to 25. Sequences are generators – values are generated on-demand (i.e. are lazily evaluated) – while lists and arrays are evaluated eagerly.generators F# uses pattern matching to bind values to names. Pattern matching is also used when accessing discriminated unions - the union is value matched against pattern rules and a rule is selected when a match succeeds. F# also supports Active Patterns as a form of extensible pattern matching. It is used, for example, when multiple ways of matching on a type exist.pattern matching F# supports a general syntax for defining compositional computations called computation expressions. Sequence expressions, asynchronous computations and queries are particular kinds of computation expressions. Computation expressions are an implementation of the monad pattern.

9 DIVE INTO F#: CONTINUATION IMPERATIVE PROGRAMMING F# support for imperative programming includes for loops while loops arrays, created with the [|... |] syntax hash table, created with the dict [... ] syntax or System.Collections.Generic.Dictionary type). Values and record fields can also be labelled as mutable. For example: In addition, F# supports access to all CLI types and objects such as those defined in the System.Collections.Generic namespace defining imperative data structures.

10 DIVE INTO F#: CONTINUATION OBJECT PROGRAMMING F#, like other CLI languages, can use CLI types and objects through object programming. F# support for object programming in expressions includes: Dot-notation (e.g., x.Name) Object expressions (e.g., { new obj() with member x.ToString() = "hello" }) Object construction (e.g., new Form()) Type tests (e.g. x :? string) Type coercions (e.g., x :?> string) named arguments (e.g., x.Method(someArgument=1)) Named setters (e.g., new Form(Text="Hello")) Optional arguments (e.g., x.Method(OptionalArgument=1)

11 DIVE INTO F#: CONTINUATION IMPERATIVE PROGRAMMING Support for object programming in patterns includes Type tests (e.g., :? string as s) Active patterns, which can be defined over object types F# object type definitions can be class, struct, interface, enum or delegate type definitions, corresponding to the definition forms found in the C#. For example, here is a class with a constructor taking a name and age, and declaring two properties.

12 DIVE INTO F#: CONTINUATION ASYNCHRONOUS PROGRAMMING F# supports asynchronous programming through asynchronous workflows. An asynchronous workflow is defined as a sequence of commands inside an async{... }, as in

13 DIVE INTO F#: CONTINUATION PARALLEL PROGRAMMING Parallel programming is supported partly through the Async.Parallel, Async.Start and other operations that run asynchronous blocks in parallel. Parallel programming is also supported through the Array.Parallel functional programming operators in the F# standard library, direct use of the System.Threading.Tasks task programming model, the direct use of.NET thread pool and.NET threads and through dynamic translation of F# code to alternative parallel execution engines such as GPU code.

14 DIVE INTO F#: CONTINUATION UNITS OF MEASURE The F# type system supports units of measure checking for numbers. The units of measure feature integrates with F# type inference to require minimal type annotations in user code. Unit of measurement A unit of measurement is a definite magnitude of a physical quantity, defined and adopted by convention or by law, that is used as a standard for measurement of the same physical quantity. Any other value of the physical quantity can be expressed as a simple multiple of the unit of measurement. The following line defines the measure ml (milliliter) as a cubic centimeter (cm^3).

15 DIVE INTO F#: CONTINUATION METAPROGRAMMING F# allows some forms of syntax customization to support embedding custom domain-specific languages within the F# language itself, particularly through computation expressions. F# includes a feature for run-time meta-programming called quotations. A quotation expression evaluates to an abstract syntax representation of F# expressions. A definition labelled with the [ ] attribute can also be accessed in its quotation form. F# quotations are used for various purposes including to compile F# code to JavaScript and GPU code.

16 DIVE INTO F#: CONTINUATION INFORMATION RICH PROGRAMMING F# 3.0 introduced a form of compile-time meta-programming through statically extensible type generation called F# type providers. F# type providers allow the F# compiler and tools to be extended with components that provide type information to the compiler on-demand at compile time. F# type providers have been used to give strongly typed access to connected information sources in a scalable way, including to the Freebase knowledge graph. The combination of type providers, queries and strongly typed functional programming is known as information rich programming.

17 DIVE INTO F#: CONTINUATION AGENT PROGRAMMING F# supports a variation of the Actor programming model through the in-memory implementation of lightweight asynchronous agents. For example, the following code defines an agent and posts 2 messages:

18 DIVE INTO F#: CONTINUATION EXAMPLES - HELLO WORLD

19 DIVE INTO F#: CONTINUATION EXAMPLES - A PERSON CLASS WITH A CONSTRUCTOR TAKING A NAME AND AGE AND TWO PROPERTIES

20 DIVE INTO F#: CONTINUATION EXAMPLES - THE FACTORIAL FUNCTION FOR NON-NEGATIVE 32-BIT INTEGERS (OFTEN USED TO DEMONSTRATE THE SYNTAX OF FUNCTIONAL LANGUAGES)

21 DIVE INTO F#: CONTINUATION EXAMPLES - ITERATION EXAMPLES

22 DIVE INTO F#: CONTINUATION EXAMPLES - FIBONACCI EXAMPLES

23 DIVE INTO F# DEVELOPMENT TOOLS The Visual F# tools from Microsoft include full IDE integration in Visual Studio. With the language service installed, Visual Studio can be used to create F# projects and the Visual Studio debugger used to debug F# code. In addition, the Visual F# tools comes with a Visual Studio-hosted REPL interactive console that can be used to execute F# code as it is being written. F# can be developed with any text editor. Specific support exists in editors such as Emacs. WebSharper is a framework for cross-tier JavaScript and HTML5 development with F#. MonoDevelop is an integrated development environment supporting F# programming on Linux, Mac and Windows including support for the interactive console as used by Visual Studio. SharpDevelop has supported F# since version 3.0. MBrace is a framework and runtime for the development of applications with F# for the cloud. LINQPad has supported F# since version 2.x. Xamarin Studio supports F# since version 3.0.

24 FEATURES NOT AVAILABLE IN OTHER PROGRAMMING LANGUAGES IN.NET WORLD? Algebraic datatypes Pattern matching Type inference Succinct syntax Sequence expressions Asynchronous workflows Units of measure

25 FEATURES: CONTINUATION ALGEBRAIC DATA TYPES Conversely, F# has a full support for ADT's and makes it possible to describe business entities more accurately in terms of data structures reducing the chance of misinterpreting the data and resulting in higher quality of code. Basically in F# one can avoid unwanted situations by making them unpresentable, that is simply by leaving no chance to write inappropriate code.

26 FEATURES: CONTINUATION TRANSFORMATIONS VS. MUTATIONS On the other hand, F# encourages using transformations rather than mutations. A transformation doesn't modify the object, and thus doesn't change the state. It just creates a new object and passes it on keeping the original object intact. Which means that the object can always be in one single state that was given to it at birth. Having to deal with just one state greatly simplifies the development process and reduces the amount of code, because whenever we see an object we know it's in the only state possible and it's safe to use it without doing extra checks.

27 FEATURES: CONTINUATION EXPRESSIONS VS. STATEMENTS Program in F# is essentially a big expression (a formula) composed of smaller expressions that map the input values to some output values. The order in which different parts of the expression are evaluated is not important, because in side-effects-free calculation model the order of evaluation doesn't change the result. So switching from using statements to programming with expressions results in safer code free of bugs caused by the wrong order of statements.

28 FEATURES: CONTINUATION KEEPING DATA AND LOGIC SEPARATELY Functional programming suggests that you don’t mix data with logic; as a result the serialization to JSON or XML is much easier and straightforward.

29 FEATURES: CONTINUATION TYPE INFERENCE AND CONCISE SYNTAX F# has an advanced type inference system that deduces types of values from how these values are used, so in most cases it is recommended to omit types in your code letting F# to figure them out. Having to type less results in higher productivity and better maintainability of the code. Some people find F# syntax more elegant and easier to read.

30 FEATURES: CONTINUATION UP-DOWN LEFT-TO-RIGHT EVALUATION In F# the order of declaration is strictly up to the order of files, so you cannot refer to or use something that hasn't been declared before. While this may seem an unnecessary complication, it is rather a good thing, because it imposes more discipline on the developer and makes his intentions explicit.

31 FEATURES: CONTINUATION INHERENT TESTABILITY AND GOOD DESIGN Modern object oriented design principles known as SOLID can be easily violated in C#; that’s why it is important to know them and follow them closely. However, the very same principles are in the nature of the functional programming. So the F# language encourages you to code the right way from the start and it takes some deliberate effort to mess things up in F#.

32 FEATURES: CONTINUATION RUNNING IN PARALLEL AT LOW EXPENSE F# eliminates the problem when 2 threads are trying to modify the same object simultaneously completely by disallowing objects from being modified after creation, whenever a change is needed a new object is constructed by applying a transformation on the original object. This means that the code written in F# can naturally be run in parallel with little additional efforts.

33 FEATURES: CONTINUATION BETTER MODULARITY A basic unit of logic in F# is a function. Functions in F# are first-class citizens which can be passed as parameters to other functions and be returned as a result of a function. Writing functions takes less effort than writing classes. This enables a finer level of modularity and more sophisticated composition scenarios at lower cost.

34 FEATURES: CONTINUATION FOCUSING ON SOLVING GENERIC PROBLEMS INSTEAD OF SPECIFIC ONES F# encourages you to use generic data-types instead of concrete ones. The same algorithm written for generics will work on different concrete types like numbers or strings or complex objects. Having just one generic implementation for different types makes the code more reusable and leaves less room for a mistake.

35 FEATURES: CONTINUATION TYPE PROVIDERS F# has a unique mechanism for working with heterogeneous sources of data in a convenient unified way called type providers. Type providers abstract the implementation details of various sources of data, ensure the type safety and expose standard well-defined interfaces. They also feature on-demand type discovery when new types are loaded only when needed. There is a repository of type providers to various sources of data, which is constantly updated and contributed to by the community. Type providers enable information-rich programming by making it easier to consume the data from different sources.

36 FEATURES: CONTINUATION STANDARD TOOLS FOR GENERATING PARSERS F# has 2 standard tools FsLex and FsYacc for generating parsers based on a formal grammar. These tools are named after Lex and Yacc the popular parser generator utilities in the Unix world. FsLex and FsYacc have rich diagnostic capabilities and can be incorporated in the build process helping to streamline the development.

37 FEATURES: CONTINUATION OTHER LANGUAGES SIMILAR TO F# Standard ML OCaml Haskell Clojure

38 FEATURES: CONTINUATION NULL Options Instead of NULL References F# doesn't allow NULL's, instead the options have to be used. They guarantee safety and serve the same purpose of representing missing objects just like NULL's do in C#.

39 USE F# ON LINUX OPTION 1: USE THE F# DEBIAN/UBUNTU PACKAGES F# is available as a Debian package. It is highly recommended (albeit optional) that you add the Xamarin Mono package repository to your sources to get a much more up-to-date version of the Mono runtime used by F#. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono- xamarin.list For Ubuntu 12.04 and 12.10 you will need an additional repository as well, see these instrutions. Older F# and Mono packages are available in Debian testing and Ubuntu 14.04 (trusty/universe). Regardless of which repository source you are using, proceed to install the fsharp & mono packages sudo apt-get update sudo apt-get install mono-complete fsharp

40 USE F# ON LINUX OPTION 2: BUILD AND INSTALL THE F# RUNTIME, COMPILER AND TOOLS Get Mono, the cross-platform, open source.NET runtime implementation used by F#. Preferably use a package from your distribution or Xamarin. If this is not possible, install from source by following these instructions. Note that if you are installing to a private prefix, follow these instructions and ensure LD_LIBRARY_PATH includes the “lib” directory of that prefix location and PKG_CONFIG_PATH includes the “lib/pkgconfig” directory of that prefix location, e.g. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/user/mono/lib/ export PKG_CONFIG_PATH=/home/user/mono/lib/pkgconfig/ Build and install the F# Compiler (open edition) from source. If using a VM or other memory-constrained system, be aware that errors during compilation may be due to insufficient memory (in particular error 137). sudo apt-get install autoconf libtool pkg-config make git automake git clone https://github.com/fsharp/fsharp cd fsharp./autogen.sh --prefix /usr make sudo make install

41 USE F# ON MAC OSX Option 1: Install F# with Xamarin Studio Xamarin Studio is a free IDE for general purpose development, with freemium add-ins for mobile development. Install Xamarin Studio Option 2: Install F# alone To use the F# command-line compiler and tools on Mac OSX: Install Mono. Use version 4.2.0 or later. Option 3: Install F# via Homebrew F# is installed as part of the Mono homebrew formula: brew install mono

42 USE F# ON MAC OSX: CONTINUATION OPTION 4: INSTALL F# (64-BIT) To use the F# command-line compiler and tools on Mac OSX in 64-bit mode: Get and build a 64-bit installation of the runtime used by F# from source. Set the “–prefix” flag, e.g. “–prefix=/mono64” git clone https://github.com/mono/mono cd mono./autogen.sh --prefix=/mono64 --enable-nls=no make sudo make install

43 USE F# ON MAC OSX: CONTINUATION OPTION 4: INSTALL F# (64-BIT) Compile F# from source Set the “–prefix” flag, e.g. “–prefix=/mono64” git clone https://github.com/fsharp/fsharp cd fsharp./autogen.sh --prefix=/mono64 make sudo make install

44 USE F# ON MAC OSX: CONTINUATION OPTION 4: INSTALL F# (64-BIT) When you run mono, use /mono64/bin/mono and put /mono64/bin on your path. Adjust other applications that launch mono to use this location. Xamarin Studio and MonoDevelop run applications in 32-bit mode by default. You will need to run programs from the command line to benefit from 64-bit execution.

45 GUIDE - CROSS-PLATFORM DEVELOPMENT WITH F# To get started with F#, please refer to the following documents: Getting Started with F# on Mac Getting Started with F# on Linux Getting Started with F# on Windows Getting Started with F# for iOS Programming Getting Started with F# for Android Programming

46 COMMAND LINE TOOLS You can start F# Interactive using: $ fsharpi > 1+1;; val it : int = 2 You’re off! Some common commands are: fsharpi (starts F# interactive) fsharpc file.fs (F# compiler) xbuild (builds.fsproj projects and.sln files) mono file.exe arg1... argN (runs a compiled F# program) mkbundle --static file.exe -o file (makes a static native image, including the F# runtime)

47 EXPLORING LIBRARIES MATH.NET NUMERICS Math.NET Numerics aims to be the standard open source math library for.NET Framework. It is released under the MIT license and so it can be used in both commercial and noncommercial projects, as well as modified and redistributed. This tutorial gives an introduction on how to use the various components of this library. Currently, Math.NET Numerics is the most actively developed part of Math.NET. Among others, it contains a wide range of numerical algorithms including: Types representing dense as well sparse vectors and matrices. A library containing a full set of standard linear algebra routines. A range of random number generators with different strengths. Additional features that include support for certain numerical integration and statistical functions.

48 EXPLORING LIBRARIES MATH.NET NUMERICS The Math.NET Numerics library is implemented in the safe subset of C# language, which makes it very portable. The library runs on a wide range of platforms, including Microsoft.NET Framework (for developing web, server, and desktop applications), Silverlight (for developing rich client-side and Windows Phone 7 applications), and Mono as well as Moonlight (for developing cross-platform applications). Another important feature of Math.NET Numerics is that it provides wrappers for highly-optimized native math libraries such as Intel MKL and AMD ACML. This makes it possible to utilize specialized CPU instruction sets when they are available. For example, the matrix multiplication in Intel MKL runs several times faster than portable C# code.

49 EXPLORING LIBRARIES MATH.NET NUMERICS USAGE The Math.NET Numerics library can be downloaded from the official homepage at CodePlex. The two assemblies that need to be referenced when using the library from F# are MathNet.Numerics.dll and MathNet.Numerics.FSharp.dll. When using Math.NET Numerics on others from Windows platforms, the library may need to be compiled from the source. The F# support for interactive scripting makes it more convenient to write and explore mathematical problems using an F# Script File and F# Interactive in Visual Studio. The MathNet.Numerics.dll assembly contains types for vectors and matrices, linear algebra routines, and others. TheMathNet.Numerics.FSharp.dll assembly is a small F# wrapper for Math.NET Numerics, which helps to create vectors and matrices using basic F# types such as sequences and lists. The distribution also comes with documentation (MathNet.Numerics.chm) and a comprehensive set of examples in C#.

50 EXPLORING LIBRARIES MATH.NET NUMERICS USAGE Math.NET Numerics can be referenced from an F# Script File using the following commands:

51 EXPLORING LIBRARIES MATH.NET NUMERICS CREATING AND ACCESSING VECTORS AND MATRICES The most fundamental data structures of a numerical library are types representing vectors and matrices. In Math.NET Numerics, there are four kinds of vectors and matrices for different numeric types (float, double, complex32, and 64-bit complex). The types are located in four sub-namespaces of the MathNet.Numerics.LinearAlgebra namespace. A script file typically imports vectors and matrices from one of the four sub-namespaces (Single, Double, Complex32 or Complex). The following snippet shows how to use vectors from the Double namespace. For historical reasons, F# calls the double type float, and the single type is called float32.

52 EXPLORING LIBRARIES MATH.NET NUMERICS CREATING AND ACCESSING VECTORS AND MATRICES The DenseVector type can be created using an overloaded constructor. When provided with an integer, it creates a zero vector of the specified size. The second example creates a vector of the given size with each element set to 3.0. Finally, the last two examples create a vector from the data contained in an array or another vector, respectively.

53 EXPLORING LIBRARIES MATH.NET NUMERICS CREATING AND ACCESSING VECTORS AND MATRICES A vector is a mutable type, so it is possible to change its values. The following snippet shows several examples of reading and writing vector elements:

54 EXPLORING LIBRARIES MATH.NET NUMERICS CREATING AND ACCESSING VECTORS AND MATRICES Similarly to the DenseVector type, there are also four different DenseMatrix types defined in the same four namespaces. The following listing shows how to create double matrices using the constructors of DenseMatrix.

55 EXPLORING LIBRARIES MATH.NET NUMERICS CREATING AND ACCESSING VECTORS AND MATRICES Similarly to vectors, matrices are mutable types and the elements can be accessed or modified using the indexer and methods of the Matrix type:

56 EXPLORING LIBRARIES MATH.NET NUMERICS USING F# EXTENSIONS Math.NET Numerics comes with a small assembly MathNet.Numerics.FSharp.dll that contains several extensions designed especially for F#. Functions from this assembly make creating vectors and matrices in F# even easier. After referencing the assembly, F# functionality can be found in the MathNet.Numerics.FSharp namespace:

57 EXPLORING LIBRARIES MATH.NET NUMERICS USING LINEAR ALGEBRA ROUTINES WRITING LINEAR ALGEBRA CALCULATIONS Working with linear algebra in the Math.NET Numerics library is very easy. Most of the important operations are available as members of the matrixtype. The following code example shows how to create a random matrix and calculate its inverse:

58 EXPLORING LIBRARIES MATH.NET NUMERICS USING LINEAR ALGEBRA ROUTINES WRITING LINEAR ALGEBRA CALCULATIONS An inverse matrix can be calculated using the Inverse member. Other linear algebra operations can be accessed using the same syntax. The next three snippets show how to decompose a matrix using the QR decomposition, Eigenvalue Decomposition (EVD), and Singular Value Decomposition (SVD) algorithms: The first two lines show how to calculate the QR decomposition on a dense matrix A. QR decomposition is often used for solving linear least squares problems. The following two lines show how to perform the Eigen decomposition of a matrix, which has many applications, such as face recognition and Principal Components Analysis (PCA). The final two lines show how to calculate the SVD.

59 EXPLORING LIBRARIES MATH.NET NUMERICS USING LINEAR ALGEBRA ROUTINES WRITING LINEAR ALGEBRA CALCULATIONS Math.NET Numerics uses an extensibility model that can be used to provide an alternative implementation of basic linear algebra operations. The extensibility model is based on the concept of the linear algebra provider. Technically, a provider is a.NET interface that is similar to the standard linear algebra operations defined in LAPAK. The following F# code snippet shows a part of the interface: The above listing shows that the style of parameters is very similar to that of LAPAK library.

60 EXPLORING LIBRARIES MATH.NET NUMERICS USING LINEAR ALGEBRA ROUTINES WRITING LINEAR ALGEBRA CALCULATIONS After the steps needed to compile Acml done, the Math.NET Numerics library can be configured to use the newly compiled provider. This can be done using the following snippet: The performance of the two implementations can be compared by measuring the time needed to execute the examples. The easiest way to do that is to use the #time directive in F# Interactive.

61 EXPLORING LIBRARIES MATH.NET NUMERICS USING PROBABILITY AND STATISTICS APIS The support for probability and statistics is one distinguishing features of the Math.NET Numerics library. The library contains a wide range of probability distributions, samplers, as well as functions for descriptive statistics.

62 EXPLORING LIBRARIES MATH.NET NUMERICS USING PROBABILITY AND STATISTICS APIS WORKING WITH PROBABILITY DISTRIBUTIONS Math.NET Numerics implements a large number of probability distributions, both continuous (represented using the IContinuousDistribution interface) and discrete (the IDiscreteDistribution interface). There are about thirty different univariate distributions and six multivariate distributions. Each distribution is represented by an object that has a constructor to create a distribution using the specified parameters. The distribution object implements one of the interfaces and provides properties to get the basic statistics such as the mean and standard deviation of the distribution. Both interfaces also provide a method Sample that generates a random sample from this distribution. This is a very important operation required by many algorithms such as Markov chain Monte Carlo (MCMC) methods.

63 EXPLORING LIBRARIES MATH.NET NUMERICS USING PROBABILITY AND STATISTICS APIS WORKING WITH PROBABILITY DISTRIBUTIONS The following listing creates a normal distribution and uses it to generate two random samples: The objects representing probability distributions can be found in the MathNet.Numerics.Distributions namespace. When creating a Normaldistribution, the constructor expects a mean and a standard deviation as arguments. After creating the distribution, the Sample method can be used to generate random samples.

64 EXPLORING LIBRARIES MATH.NET NUMERICS USING PROBABILITY AND STATISTICS APIS WORKING WITH PROBABILITY DISTRIBUTIONS The next snippet shows how to obtain basic statistics about the distribution: The first line shows the density value at 5.0 for normal. The second line shows how to get the right point value (7.579) given a cumulative sum (0.95) of a normal distribution. Finally, the Samples member can be used to generate an infinite sequence of random samples. The snippet uses Seq.takeand Seq.toList to take first 10 samples and convert them to an F# list.

65 EXPLORING LIBRARIES MATH.NET NUMERICS USING PROBABILITY AND STATISTICS APIS USING DESCRIPTIVE STATISTICS Another feature that the library provides is a set of extension methods for calculating descriptive statistics of data collections such as mean, variance, maximum, and minimum. The methods are implemented in a Statistics class in the MathNet.Numerics.Statistics namespace. They can be called either directly or using the dot-notation on any type that implements IEnumerable : The snippet uses the normal distribution from the previous section to generate a random sequence of numbers using a normal distribution with mean 1.0 and a standard deviation 4.0. As expected, the statistics calculated from the data using the Mean and StandardDeviation extension methods are quite close to the properties of the distribution.

66 EXPLORING LIBRARIES MATH.NET NUMERICS USING PROBABILITY AND STATISTICS APIS USING DESCRIPTIVE STATISTICS In addition, the library also provides a DescriptiveStatistics object that can be created from a collection and precomputes all the statistics at once: The listing uses the DescriptiveStatstics type to calculate the mean, standard deviation as well as numerous other statistics from the data. Since the snippet uses the same dataset, the results are the same as the values calculated using extension methods. The main benefit of using the DescriptiveStatstics type is that it processes the data only once.

67 EXPLORING LIBRARIES MATH.NET NUMERICS SUMMARY This section introduced various components of the Math.NET Numerics library. It discussed how to work with matrices and vectors using the.NET API provided by the library as well as using F# wrappers. It also demonstrated how to use linear algebra functionality of the library. The basic implementation is fully managed, but it is possible to use native libraries such as Intel MKL or AMD ACML. Finally, the section looked at several topics from the area of probability and statistics. It demonstrated how to construct and sample a distribution and how to calculate basic descriptive statistics. Some of the topics that were not covered, but are supported by Math.NET Numerics, include numerical integration and interpolation.

68 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). Accord.NET is a framework for scientific computing in.NET. The framework is comprised of multiple libraries encompassing a wide range of scientific computing applications, such as statistical data processing, machine learning, pattern recognition, including but not limited to, computer vision and computer audition. The framework offers a large number of probability distributions, hypothesis tests, kernel functions and support for most popular performance measurements techniques.

69 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). HOW TO USE LOADING DATA If you plan to load data into your application (which is most likely the case), then you should consider adding a reference to the Accord.IO library into your project. This library provides data readers for formats like Excel, comma-separated values, matlab matrix files, LibSVM and LibLinear's data formats, and others. If you are interested in loading sounds into your application, consider adding a reference to the Accord.Audio.Formats library.

70 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). TABLES(EXCEL AND EXCEL COMPATIBLE FILES (SUCH AS.CSV) To import a table from Excel or.CSV files, you can use the ExcelReader class class. To load the contents of "Sheet1" located in "worksheet.xlsx", use: In order to convert a DataTable to a framework matrix, we can use

71 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). MATRICES The framework can load any MATLAB-compatible.mat file using the MatReader class. It can also parse matrices written in MATLAB, Octave, Mathematica and C#/NET formats directly from text. Examples are shown below.

72 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). MANIPULATING MATRICES The framework provides matrix manipulation routines through extension methods. Just import the Accord.Math namespace into your source file and all common.NET datatypes will be extended with several extension methods related to mathematics. Please see the Mathematics page for more examples and details.

73 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). MANIPULATING MATRICES One common task in matrix manipulation is to decompose a matrix into various forms. Some examples of the decompositions supported by the framework are listed below. Those decompositions can be used to solve linear systems, compute matrix inverses and pseudo-inverses and extract other useful information about data.

74 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). DATA PREPROCESSING Before attempting to learn a machine learning model, a good practice is to preprocess, normalize and clean your data. One of the simplest ways to normalize data is by transforming them to Z-scores. In order to transform your data to Z-Scores, you can use the following method: In case you would like to subtract the mean from your data, you can use the Center method And to divide by the standard deviation you can use the Standardize method

75 EXPLORING LIBRARIES ACCORD.NET MACHINE LEARNING FRAMEWORK (CLUSTERING, CLASSIFICATION, REGRESSION). SOME OTHER CATEGORIES Learning from input and output pairs Finding similarity groups in data Visualizing your results

76 F# AND PYTHON PYTHON FOR.NET Python for.NET - Allows Python to be integrated into F# and C# programs Embedding Python Note: because Python code running under Python for.NET is inherently unverifiable, it runs totally under the radar of the security infrastructure of the CLR so you should restrict use of the Python assembly to trusted code. The Python runtime assembly defines a number of public classes that provide a subset of the functionality provided by the Python C API. These classes include PyObject, PyList, PyDict, etc. The source and the unit tests are currently the only API documentation.. The rhythym is very similar to using Python C++ wrapper solutions such as CXX.

77 F# AND PYTHON PYTHON FOR.NET At a very high level, to embed Python in your application you will need to: Reference Python.Runtime.dll in your build environment Call PythonEngine.Intialize() to initialize Python Call PythonEngine.ImportModule(name) to import a module The module you import can either start working with your managed app environment at the time its imported, or you can explicitly lookup and call objects in a module you import.

78 F# AND PYTHON PYTHON FOR.NET For general-purpose information on embedding Python in applications, use www.python.org or Google to find (C) examples. Because Python for.NET is so closely integrated with the managed environment, you will generally be better off importing a module and deferring to Python code as early as possible rather than writing a lot of managed embedding code. Important Note for embedders: Python is not free-threaded and uses a global interpreter lock to allow multi-threaded applications to interact safely with the Python interpreter. Much more information about this is available in the Python C API documentation on the www.python.org Website. When embedding Python in a managed application, you have to manage the GIL in just the same way you would when embedding Python in a C or C++ application.

79 F# AND PYTHON PYTHON FOR.NET Before interacting with any of the objects or APIs provided by the Python.Runtime namespace, calling code must have acquired the Python global interpreter lock by calling the PythonEngine.AcquireLock method. The only exception to this rule is the PythonEngine.Initialize method, which may be called at startup without having acquired the GIL. When finished using Python APIs, managed code must call a corresponding PythonEngine.ReleaseLock to release the GIL and allow other threads to use Python. The AcquireLock and ReleaseLock methods are thin wrappers over the unmanaged PyGILState_Ensure and PyGILState_Release functions from the Python API, and the documentation for those APIs applies to the managed versions.

80 WHAT IS R? RR is an Open Source language for statistical computing. R offers a wide range of high-quality, community- developed packages, covering virtually every area of statistics, econometrics or machine learning. It is also famous for its charting capabilities, making it a great tool to produce publication-quality graphics. R is an interpreted, dynamically typed language that is typically used from its GUI, RStudio, or command line interactive environment.RStudio

81 F# AND R R.NET R.NET enables the.NET Framework to interoperate with the R statistical language in the same process. R.NET requires the.NET Framework 4 and the native R shared libraries installed with the R environment. You can use R.NET from any language targeting.NET (it has been used at least from C#, F#, Vb.NET, IronPython). For F#, you probably should consider F# R Provider. One motivation for releasing 1.5.13 is for the RProvider to more easily manage dependency on R.NET.

82 F# AND R R.NET As of version 1.5.10 or later, R.NET binaries are platform independent. You might need to set up a small add-on workaround on some Linux distributions (CentOS a known one), but otherwise you can just move and use the same R.NET binaries across platforms. As of July 2015, NuGet is the strongly recommended way to manage dependencies on R.NET in its binary distribution form. You can find more general information about NuGet at the NuGet NuGet

83 F# AND R R.NET EXAMPLES - HELLO WORLD The following "Hello World" sample illustrates how the new API is simpler in 90% of use cases on Windows (on Linux you may need to set up an environment variable, see thereafter):

84 F# AND R R.NET You retrieve a single REngine object instance, after setting the necessary environmental variables. Even the call to REngine.SetEnvironmentVariables() can be omitted, though we'd advise you keep it explicit. SetEnvironmentVariables, on Windows, looks at the Registry settings set up by the R installer. If R.NET complains about not being able to find one of the paths to R, see. This can happen due to the many variations of states of Windows Registries and environment variables. If need be, you can override the behaviours setting the environment variables and engine initialization with your own steps. On Linux/MacOS, the path to libR.so (for Linux) must be in the environment variable {"LD_LIBRARY_PATH"} before the process start, otherwise the R.NET engine will not properly initialize. If this is not set up, R.NET will throw an exception with a detailed message giving users hints as to what to do. Read the Appendix at the end of this page if R.NET complains about your {"LD_LIBRARY_PATH"}.

85 F# AND R F# R TYPE PROVIDER The F# Type Provider is a mechanism that enables smooth interoperability between F# and R. The Type Provider discovers R packages that are available in your R installation and makes them available as.NET namespaces underneath the parent namespace RProvider. The Type Provider makes it possible to use all of R capabilities, from the F# interactive environment. It enables on-the-fly charting and data analysis using R packages, with the added benefit of IntelliSense over R, and compile-time type-checking that the R functions you are using exist. It allows you to leverage all of.NET libraries, as well as F# unique capabilities to access and manipulate data from a wide variety of sources via Type Providers.

86 F# AND R F# R TYPE PROVIDER EXAMPLES - PASSING DATA FROM R TO F# Let's say that you have some data in R and want to pass them to F#. To do that, you can use the save function in R. The following R snippet creates a simple *.rdata file containing a couple of symbols from the sample volcano data set: To import the data on the F# side, you can use the RData type provider that is available in the RProvider namespace. It takes a static parameter specifying the path of the file (absolute or relative) and generates a type that exposes all the saved values as static members:

87 F# AND R F# R TYPE PROVIDER EXAMPLES - PASSING DATA FROM R TO F# If you perform data acquisition in F# and then want to pass the data to R, you can use the standard R functions for saving the *.rdata files. The easiest option is to call the R.assign function to define named values in the R environment and then use R.save to save the environment to a file:

88 COMPARISON OF PRODUCTIVITY

89 REFERENCES F# Software Foundation and individual contributors (http://fsharp.org/)http://fsharp.org/) F Sharp (programming language) (https://en.wikipedia.org/wiki/F_Sharp_(programming_language))(https://en.wikipedia.org/wiki/F_Sharp_(programming_language)) Using Math.NET Numerics in F# (https://msdn.microsoft.com/en-us/library/hh304363.aspx)(https://msdn.microsoft.com/en-us/library/hh304363.aspx) The Accord.NET Framework (https://github.com/accord-net/framework/wiki)https://github.com/accord-net/framework/wiki) Python for.NET (http://pythonnet.sourceforge.net/readme.html)(http://pythonnet.sourceforge.net/readme.html) R.NET (http://jmp75.github.io/rdotnet/) (http://jmp75.github.io/rdotnet/ F# R Type Provider (http://bluemountaincapital.github.io/FSharpRProvider/)http://bluemountaincapital.github.io/FSharpRProvider/ Efficiency comparison of scientific computing among different languages (https://prezi.com/6wpbvnq56ddn/efficiency-comparison-of-scientific-computing-among-different- languages/)https://prezi.com/6wpbvnq56ddn/efficiency-comparison-of-scientific-computing-among-different- languages/ Scientific Computing with F# (https://confengine.com/functional-conf-2015/proposal/1436/scientific- computing-with-f)

90 THANK YOU FOR ATTENTION!


Download ppt "F# FOR SCIENTIFIC COMPUTING VITALII ZAKHAROV. QUICK INTRODUCTION TO F# F# uses an open development and engineering process. The language evolution process."

Similar presentations


Ads by Google