67
Mathematica Tech Talk Mathematica 8: An Integrated Platform for Science & Engineering Yu-Sung Chang, Ph.D. Manager, Technical Communication & Strategy Group Wolfram Table of Contents About Wolfram : An Integrated Platform for Science & Engineering Wolfram|Alpha in Mathematica Working with Mathematica 8: From Concepts to Applications Parallel Computation with Mathematica GPU Computing in Mathematica Introducing MathModelica About Wolfram Wolfram Research + Wolfram|Alpha » Founded by Stephen Wolfram in 1987 » One of the world’s most respected software companies » Pioneers in computational science and the computational paradigm » Mathematica products » Wolfram|Alpha services

8th Mathematica Technical Talk

Embed Size (px)

DESCRIPTION

hgdf

Citation preview

  • Mathematica Tech TalkMathematica 8:An Integrated Platform for Science & Engineering

    Yu-Sung Chang, Ph.D.Manager, Technical Communication & Strategy GroupWolfram

    TableofContents

    About Wolfram : An Integrated Platform for Science & Engineering

    Wolfram|Alpha in Mathematica

    Working with Mathematica 8: From Concepts to Applications

    Parallel Computation with Mathematica

    GPU Computing in Mathematica

    Introducing MathModelica

    About Wolfram

    Wolfram Research + Wolfram|Alpha Founded by Stephen Wolfram in 1987

    One of the worlds most respected software companies

    Pioneers in computational science and the computational paradigm

    Mathematica products

    Wolfram|Alpha services

  • Our Customers: Industry Recognition

    What is Mathematica?

    Compute, Develop, Deploywith One Integrated System

    Streamlined Workflow for R&D

    2 GIST.nb

  • Mathematica Advantage

    Unique Principles and Benefits: Innovative automation

    Produce expert results without expertise

    Integrated all-in-one platform

    Save time and money, and access greater innovative capabilities compared to standalone tools

    Hybrid numeric-symbolic methodology

    Get more accurate results than from any other tool

    Multi-paradigm language

    Compute as you think and save time

    Built-in knowledge

    Access the worlds largest pool of up-to-date computable knowledge

    Document centric workflow

    Document your work as you go, organize explanations, data, code, and outputs in a single file

    Mathematica:Quick Tour

    Integrated All-in-one Platform From development to deployment: Simplified workflow

    GIST.nb 3

  • Integrated All-in-one Platform From Aeronautics to Optics: Industries

    Integrated All-in-one Platform From business graphics to wavelets: Specialist functionality. Built-In.

    Business Graphics

    Control Systems

    Statistics

    Finance

    Image Processing

    Geographic Information Systems

    Wavelet Analysis

    GPU Programming

    High Performance Computing

    Powerful Computation Engine Symbolic mathematics: Fundamentals of Mathematica

    Solvea x2 b x c 0, xSolvea x4 b x c 0, x

    1

    x10 1x

    TraditionalForm1

    x10 1x

    4 GIST.nb

  • Powerful Computation Engine Numerical computation: Any precision. Compute with confidence.

    f

    t 20 Expxx x;

    In[10]:= TabViewWithopts Frame True, PlotStyle Thick,ImageSize Scaled1, AspectRatio 1 3, "Machine Precision"

    PlotSinf, t, 45, 30, WorkingPrecision MachinePrecision, opts,"High Precision" PlotSinf, t, 45, 30, WorkingPrecision 200, opts,

    LabelStyle "Text", FrameMargins 30, ImageSize Scaled.5

    Powerful Computation Engine Numerical computation: Exact arithmetic matters.

    45 5 1

    45

    NestListFunctionx, 5 x 1 x, 45 , 30

    Compare

    Powerful Computation Engine Numerical computation: Hybrid symbolic-numeric methods

    NIntegrate 1x CosLogx

    x , x, 0, 1

    Plot 1x CosLogx

    x , x, 0, 1, PlotRange 15, Frame True, ImageSize Large

    GIST.nb 5

  • Powerful Computation Engine Performance: Linear algebra benchmark

    Powerful Computation Engine Performance: Parallel computing made easy

    ParallelMapColorize, ColorFunction "SunsetColors" &, , , ,

    , , , , , , , , , , , ,

    How it works

    Powerful Computation Engine Integration and automation: Simulating a model

    6 GIST.nb

  • Visualization Visualize everything: From functions to everyday data

    Visualization Visualize adaptively: Quality with performance

    Interactivity & Interfaces Manipulate: Instant magic

    ManipulatePlotfk x, x, 0, 10, FillingStyle c, Filling 0, ImageSize 200,k, 1, 10, f, Sin, Cos, Tan, Sec, Sinc, c, Yellow

    Interactivity & Interfaces Easy and automated: Direct, declarative GUIs

    GIST.nb 7

  • Interactivity & Interfaces Platform independent: No extra work required

    Interactivity & Interfaces Fully customizable: Application quality

    Data Sources & Connectivity Access data in any format

    Excel format

    Data formats

    Multimedia formats

    Web standards (HTML, XML, etc.)

    Try it:

    8 GIST.nb

  • Data Sources & Connectivity Connect to databases and services

    DatabaseLink

    J/Link

    Web services

    Dynamic library

    Data Sources & Connectivity Access computable data

    CountryData"Kenya", "GDP"CountryData"Kenya", "Flag"

    Try this on Internet: GDP changes of G8 countries, since 1970Google search

    With Mathematica...

    Development & Deployment Development options

    The most sophisticated programming language for scientific and technical computing

    Comprehensive Built-in IDE features

    Wolfram Workbench: Eclipse-based enterprise-class tool

    Development & Deployment Deployment options

    Interactive documents [Documentation] Presentation & report generation

    Interactive application: Mathematica Player [Demonstrations] Web application: Mathematica Plug-in

    Web application: webMathematica

    Distributed computation: gridMathematica

    Code generation & compilation

    GIST.nb 9

  • Development & Deployment Case study: Real-world develop & deploy

    Comprehensive Resources & Support

    10 GIST.nb

  • Wolfram:The Company whereComputation meets Knowledge

    Mathematica:Unique Principles,Unique Advantages

    Mathematica: Unique principles, unique advantages

    Automation

    Integrated all-in-one platform

    Hybrid numeric/symbolic methodology

    Multi-paradigm language

    Built-in knowledge

    Document-centric workflow

    Get started today! Suggestions

    Download trials

    Network license

    Technical demos

    Free webniars

    Try Wolfram|Alpha

    And more...

    GIST.nb 11

  • Mathematica Tech TalkWolfram|Alpha in Mathematica

    Image Stephen Wolfram Blog

    What is WolframAlpha?

    WolframAlpha is NOT a search engine

    It dynamically computes answers based on avast collection of built-in data and algorithms

    Its knowledge is continually expanding:q adding more curated dataq hooking up real-time feedsq implementing new algorithmsq building new generalized grammarto capture the natural language

    It contains millions of lines ofMathematica code

    Long-term goal is to make all systematicknowledge immediately computable andaccessible to everyone

    Wolfram|Alpha API...access the Wolfram|Alpha platform from other applications

    A few examples of Wolfram|Alpha API usage on mobile iOS and Android platforms

    Wolfram|Alpha Integration in Mathematica...how does it work?

    12 GIST.nb

  • ! Requirement : Internet connection

    Four methods to access produced by double equal (= =) at beginning of input

    Performs a full Wolfram|Alpha query, producing fully formatted result similar to Wolfram|Alpha web page withtext, graphics, and even interactive elements

    derivative of f of x

    produced by a single equal (=) at beginning of input

    Uses free-form linguistics to give a single result in native Mathematica syntax if possible. The result can be

    expanded to the full Wolfram|Alpha output. For queries that do not have a Mathematica form, also returns

    the Wolfram|Alpha interpretation in addition to the result.

    derivative of f of x

    produced by += anywhere in the inputEmbeds free-form input into a Mathematica expressions for further computation.

    Manipulate derivative of f of x Dfx, x

    ,

    f, Sin, "Derivative of", Sin, Cos, Tan, Csc, Sec Magnify

    WolframAlpha - Mathematica functionThe function behind the whole Wolfram|Alpha integration including the three cases above; programmaticallygathers information from the Wolfram|Alpha API and provides the ability to choose specific outputs and formatsdepending on its arguments.

    Earthquakes in Japan:

    Expand gamma function in series:

    Molecular plot of chemical data:

    Comparison of price history:

    Double pendulum - interactive app from Wolfram|Alpha:

    Fractal dragon curve - interactive app using Wolfram|Alpha function:

    press double equal (= =) at beginning of input

    Performs a full Wolfram|Alpha query, producing fully formatted result similar to Wolfram|Alpha web page withtext, graphics, and even interactive elements

    hello

    figure 8 knot

    Solvex2 y2 y2

    x2 x2 1

    x2 x y3 y

    3

    x x y yx 2 y

    2 1 0, x

    GIST.nb 13

  • plot x2 y2+ y2x2- x2- 1

    x2+ x y3+ y3

    x- x y - y

    x+2 y2-1

    integrate 1/(1+x^3)WolframAlpha"integrate 11x^3",IncludePods "IndefiniteIntegral", AppearanceElements "Pods",TimeConstraint 30, Automatic, Automatic, Automatic,PodStates "IndefiniteIntegral__Show steps"plot tan^-1((2 x - 1)/Sqrt[3])/Sqrt[3]

    sun activitysundata WolframAlpha"sun activity",

    "SunspotsPartialTimeSeries:SpaceWeatherData", 1, "ComputableData";DateListPlotsundata, AspectRatio 1 8, Joined True, Filling 0WaveletScalogramContinuousWaveletTransformsundataAll, 2

    press single equal (=) at beginning of inputUses free-form linguistics to give a single result in native Mathematica syntax if possible. The result can be

    expanded to the full Wolfram|Alpha output. For queries that do not have a Mathematica form, also returns

    the Wolfram|Alpha interpretation in addition to the result.

    figure 8 knot

    knot 6 3

    figure 8 knot braid

    figure 8 knot braid image

    population of england historyDateListLogPlot

    integrate bessel j2

    take derivative of %

    simplify %

    cellular automaton 3-color, range 2, totalistic code 151117WolframAlpha"cellular automaton 3color, range 2, totalistic code 151117","RandomInitialConditions", 1, "Content"cellular automaton 3-color, range 2, totalistic code 151117

    ArrayPlotCellularAutomaton151 117, 3, 1, 2,SeedRandom4234; RandomInteger2, 801, 300, All,

    Frame False, PixelConstrained 1, ColorFunction "TemperatureMap"

    press += anywhere in the inputEmbeds free-form input into Mathematica expressions for further computation.

    14 GIST.nb

  • solar system planets

    GridTranspose solar system planets AstronomicalData"Planet"

    ,

    solar system planets mass

    AstronomicalData"Planet", "Mass" , Frame All

    PlotEvaluate integrate spherical bessel j2 IntegrateSphericalBesselJ2, x, x

    ,

    integrate spherical bessel j3

    IntegrateSphericalBesselJ3, x, x .

    x a , a, 30, 30, Filling 0

    Manipulate complete graph n CompleteGraphn

    , n, 3, 15, 1

    AnimateAppendYellow, SpecularityWhite, 20,

    Opacity.5, & cube 10-compound PolyhedronData"CubeTenCompound"

    ,

    Boxed False, ViewPoint DynamicCoss, Sins, 1, SphericalRegion True,s, 0, 2 Pi, AnimationRate .1

    WolframAlpha[] - Mathematica functionThe function behind the whole Wolfram|Alpha integration including the three cases above; programmaticallygathers information from the Wolfram|Alpha API and provides the ability to choose specific outputs and formats.

    WolframAlpha"query", " format"gives the available data in the specified format:

    FullOutput same asResult same asInlineInput same as

    MathematicaForms

    PodPlaintext WolframAlpha"query", podids, dataformats

    returns one or more individual pieces of data:

    GIST.nb 15

  • More information? WolframAlpha

    Native Graphical Outputs...and how to change their properties

    show transparent red sphere and 2 blue conesGraphics3DOpacity0.7, Orange, Sphere,

    Green, ArrayTranslateCone, 2 1, 2, 0 & , 2, 0double pendulum

    image

    m1

    m2

    q1

    q2

    l1

    l2 ;

    HeadimageGraphics

    add red dashed gridlines and axes to image

    add frame

    caffeine 3D structure

    image1 ; image2 ;

    InputFormimage1ManipulateShowimage2 . Spherel_, _ Spherel, r,image1, ImageSize 500

    , r, 0, 170ManipulateShowimage2 .RGBColorx_, y_, z_ RGBColorx, y, z, o, GrayLevelx_ GrayLevelx, o,

    image1, ImageSize 500, o, 0, 1

    Clearimage, image1, image2

    Tackling Applied Problems

    16 GIST.nb

  • ... and integrating built-in interactive tools into the workflow

    aMAZEing Image Processing in Mathematica - by Jon McLoone

    ;

    image rotate 5 degrees counterclockwiseInput interpretationImageRotate, 5Degree

    image adjust

    adjust

    Input interpretationImageAdjust

    binarizeInput interpretationBinarize

    delete small componentsInput interpretationDeleteSmallComponents

    thinning Input interpretationThinning

    pruning Input interpretationPruning

    GIST.nb 17

  • blur Input interpretationBlur

    add % to image imgInput interpretationImageAddimg,

    txt recognize text

    TextRecognize

    speak txt Input interpretationSpeaktxt

    count occurrences of "fish" in txt Input interpretationStringCounttxt, "fish"

    take first 8 characters of txt Input interpretationStringTaketxt, 8

    speak %Input interpretationSpeak

    ;

    recognize textInput interpretationTextRecognize

    separate colorsInput interpretationColorSeparate

    18 GIST.nb

  • take first element of % Input interpretation1

    sharpen Input interpretationSharpen

    recognize textInput interpretationTextRecognize

    speak %Input interpretationSpeak

    GIST.nb 19

  • Conclusion...advantages of using Wolfram|Alpha integration

    First and foremost

    And much more... Learn Mathematica syntax by entering a query in English and then

    viewing it as precise Mathematica commands. Easily specify plot frames, styles, gridlines, and more in natural

    language. Perform many specialist operations in fields as diverse as image

    processing and finance using free-form instructions. Easily refer to the results of previous computations in free-form

    commands. Get results that can be used immediately in visualizations or

    computations as native Mathematica or full Wolfram|Alpha-style layout.

    Use WolframAlpha function to programmatically gather information from the Wolfram|Alpha API; provides the ability to choose specific outputs and formats.

    Save a trip to documentation Access a large variety of continually updated examples on the

    Wolfram|Alpha website

    20 GIST.nb

  • Mathematica Tech TalkFunctional Programming with Mathematica

    Everything is an Expression Basic Structure

    An expression consists of a head, and then a sequence of elements.

    Headelem1, elem2, elem3,

    Operators as Shorthand

    All operators in Mathematica are shorthand for an expression, using the function FullForm we can see how + is interpreted.FullForma b

    Really Everything, Documents and MoreEven this presentation is actually an expression. Documents have head Notebook and then the cells are all Cellexpressions. This means that once you know how to work with expressions, you are able to work with everythingwithin the Mathematica system.

    Evaluation of Expressions Standard Evaluation

    Evaluation is the core operation done by Mathematica. It will attempt to evaluate expressions based on all defini-tions it knows.

    If we return to our simple example, no additional definitions are known.a b

    But if we set the variables to an integer,a 2a b

    Now we see that the definition for a has been used, so we do the same for b.b 5a bNow that we see all definitions are used, and we get the result as an output.

    This application of definitions is done with infinite recursion, and the results propagate back up through theevaluation.

    Modifying EvaluationThe standard evaluation model can also be altered, most commonly with what we call attributes. The simplestexample is when using a function like Plot.

    PlotSinx, x, 0, 2 AttributesPlotSo we see that Plot has the attribute HoldAll which prevents normal evaluation of all its elements.

    GIST.nb 21

  • Lists, the Functional WorkhorseLists are Mathematicas arrays; as in most programming environments many operations come down to manipula-tion of these data structures.

    Lists (and in fact all expressions in Mathematica) are 1-indexed; using negative indexes will start counting from theend of the list.

    ConstructingThere are a number of functions for building lists in Mathematica, most commonly used are Table and Range.

    Range is really for generating lists of numbers, Table is for lists of more arbitrary expressions.Range10TableExpandx y^i, i, 1, 5

    Manipulation and ExtractionThe Part function (operator is listi ) is the most common way to extract parts of a list.ls Range10ls3

    There is also a span operator.ls2 ;; 6Along with Part there are a number of other functions designed for extraction of parts and elements of lists, these

    include: First, Last, Most, Rest, and Take.

    Procedural to Functional ProgrammingMathematica is a multi-paradigm language, and while procedural programming is supported, it is better to use thesystems native paradigm.

    Lose the LoopWeve already seen that there are specific functions for creating lists, but its common for people to use a proceduralapproach to this.

    a ;Doa Appenda, i, i, 1, 10a

    But when we compare for a larger list, we see a big performance penalty.a ;Doa Appenda, i, i, 1, 20 000; TimingRange20 000; Timing

    The same story plays out when we want to do operations on a list.Lengthab ;Dob Appendb, ai^2, i, Lengtha; Timing

    If we use the Map function this simply becomes, and runs much faster.c Map^2 &, a; Timing

    We can do even better, when we realize that this kind of operation is in fact a list con-

    22 GIST.nb

  • struction problem, so if we reframe using Table, we skip the whole need for two lists, and just construct one thing.d Tablei^2, i, 20 000; TimingThe same kind of reframing of a problem can be done with For loops as well.

    Conditional ProgrammingThe classic conditional constructs are all supported in Mathematica, but as with loops, they can be reframed into afunctional form.

    First we will set a test list.ls Range1000 000;

    Now if we wanted to square only the even numbers in this list we could create a func-tion, and then test this function.square1n_ : IfEvenQn, n^2square1 ls; Timing

    We can also use a conditional statementsquare2n_ : n^2 ; EvenQnsquare2 ls; Timing

    The third way that we could write this would be to use a predicate on the argument.square3n_?EvenQ : n^2square3 ls; TimingThis clearly has the performance advantage, but it also is the easiest code to read and understand. You know just ata glance that the body will only be executed if the input is even.

    A similar approach could be used to replace a Which statement, which would just result in several definitions to your symbol.oddCubeEvenSquare1n_ : WhichEvenQn, n^2, OddQn, n^3oddCubeEvenSquare1 ls; TimingoddCubeEvenSquare2n_?EvenQ : n^2;oddCubeEvenSquare2n_?OddQ : n^3;oddCubeEvenSquare2 ls; TimingSo we see the same advantage in both speed and readability of this code. This second form also makes debuggingmuch easier.

    Patterns and RulesPatterns are a way to represent classes of expressions, can also be thought of as a way to symbolically describe atype of expression.

    Extracting Parts with PatternsThe simplest test is to look at the head of an expression; here we can extract the integers.

    Cases3, 4, x, x^2, x^3, _Integer

    Next we can look at expressions that contain powers of x.Cases3, 4, x, x^2, x^3, x^_Related functions to Cases are Position, Count, and Select.

    GIST.nb 23

  • Transforming Expressions with RulesRules allow you to actually change something that matches a certain pattern; here we modify the above example toextract the actual power.

    Cases3, 4, x, x^2, x^3, x^power_ power

    More powerful is the ability to do this in place in the original expression or list. For that we use the function ReplaceAll in its short form.3, 4, x, x^2, x^3 . x^power_ power

    Patterns and FunctionsWe already saw using patterns in function definitions when we were looking at conditionals.

    square1n_ : IfEvenQn, n^2

    Means square1 with one expression as an argument, we could easily add more.square1n_, m_ : n^2 m^2This allows the same function name to be overloaded for many different types of input.

    Patterns extend along with expressions, so there really is no limit to what you can match with a pattern.

    24 GIST.nb

  • Mathematica Tech TalkWorking with Mathematica 8:From Concepts to Applications

    Financial Analysis

    Mathematica as a free-form exploration environment

    Financial AnalysisIn this finance example, we can begin by asking Wolfram|Alpha for stock performances, say Apple Computer. Youcan obtain the ticker data by selecting the Computable Data option from the pod.

    appleWolframAlpha"apple", "PriceHistory", 1, "ComputableData",PodStates "PriceHistory__Last 5 years"

    Financial Analysiswaap WolframAlpha"Apple", "PriceHistory", 1, "ComputableData",

    PodStates "PriceHistory__Last 5 years"1;

    We can change the company name from Apple to AIG and obtain the price data.waaig WolframAlpha"AIG", "PriceHistory", 1, "ComputableData",

    PodStates "PriceHistory__Last 5 years"1;

    We can use the code below to capture the price history and visualize the data immedi-

    GIST.nb 25

  • ately. Notice the big difference in the price history of these two stocks. This is why the PairedHistogram shows a big difference in their price distributions.dates IntersectionwaapAll, 1, waaigAll, 1;pa FlattenPositionwaapAll, 1, & dates ;pg FlattenPositionwaaigAll, 1, & dates;app waappaAll, 2;aigp waaigpgAll, 2;GraphicsRowListLinePlotapp, aigp, PlotLabel

    RowStyle"Apple ", ColorData11, Style"AIG", ColorData12,Ticks None, Automatic, PairedHistogramapp, aigp, Ticks None,ChartLabels "Apple", "AIG", ImageSize Large

    Financial AnalysisWe can now calculate the continuously compounded return on the two stocks:

    returnsdata_ : LogLast First & Partitiondata, 2, 1;apcr, aigcr returns app, aigp;LabeledGraphicsGridListLinePlotapcr, aigcr, PlotRange All, PlotLabel Row

    Style"Apple ", ColorData11, Style"AIG", ColorData12,PairedHistogramapcr, aigcr, Ticks None, ChartLabels

    "Apple \nDaily Return", "AIG \nDaily Return",ImageSize Large, "Returns and their distributions"

    Surprisingly, with such a dispersion in stock price distribution, the daily returns distributions are almost identical!Small but negative compounded return does make a big difference is AIGs stock price.

    Since investors tend to hold more than one stock their portfolios, we can easily measure the correlation between these two stock returns.cor Correlationapcr, aigcr

    We can also visualize the correlation of these two stocks:returns PartitionRiffleapcr, aigcr, 2;lm LinearModelFitreturns, x, x;ShowListPlotreturns, PlotLabel

    Columnlmx, "R2 " ToStringlm"RSquared", Alignment Center,AxesLabel "Apple", "AIG", Plotlmx, x, .07, .07, PlotStyle Red

    Notice the R2 is the correlation squared.cor^2Since the correlation coefficient is less than 1.0, we have a combination of imperfectly correlated risky assets. Theimplications of this fact for risk is central to an understanding of the effects of diversification.

    Financial AnalysisSay we have some attributes such as the expected return and standard deviation about two assets. We can constructa hypothetical model to show their performance in terms of the standard deviation of the portfolio:

    26 GIST.nb

  • erc 8;ers 10;stdc 5;stds 15;portstd2cr_ : Table

    Sqrtw2 stdc2 1 w2 stds2 2 w 1 w cr stdc stds, w, Range0, 1, 0.001;porter Tablew erc 1 w ers, w, Range0, 1, 0.001;datalcr_ : Transposeportstd2cr, porter;ManipulateListPlotdatalcr, PlotRange 4, 8, 7.5, 10, Frame True,

    ImageSize 200, FrameLabel "Standard Deviation", "Expected Return",cr, 1, "Correlation between returns", 1, 0, .01, Appearance "Open",SaveDefinitions True

    The blue line is where the efficient frontier lies. It represents the edge of feasible combinations of risk and return.When the correlation between these two assets is 1.0, there is no risk reduction. As the correlation decreases, wecan achieve a portfolio with the highest expected return given the lowest standard deviation. Therefore a smallcorrelation is a strategy for diversification. That is what investors are trying to achieve - low risk, high return. Thismodel turns out to be the foundation of the famous CAPM model that is widely used in the financial industry today.

    How does Apple and AIG compare with the hypothetical model? If we use the historical return as their expected return, we get:erap Mean100 apcr;eraig Mean100 aigcr;stdap StandardDeviation100 apcr;stdaig StandardDeviation100 aigcr;craa Correlation100 apcr, 100 aigcr;portstdaa TableSqrtw2 stdap2 1 w2 stdaig2 2 w 1 w craa stdap stdaig,

    w, Range0, 1, 0.001;porteraa Tablew erap 1 w eraig, w, Range0, 1, 0.001;dataaa Transposeportstdaa, porteraa;ListPlotdataaa, PlotRange 0, 5, 0, .15, Frame True,FrameLabel "Standard Deviation", "Expected Return"

    A portfolio with just Apple and AIG, the expected return is lower.

    Of course, this example leads into what is known as asset allocation. Jason Cawley and Roger Brown have alreadycontributed demonstrations that illustration this point. You can download the free source code from demonstra-tions.wolfram.com.

    Medical Image Processing

    Mathematica as a development environment

    Medical Image ProcessingThe task is to analyze the performance of needle-free injection devices that fire powdered drug particles into theskin on a supersonic gas shock-wave. We are trying to understand the penetration characteristics on a test mediumby photographing thin slices of target under a microscope and measuring the locations of the particles.

    The challenge is to do a good job of identifying the particles, and to give me manual over-ride for any mistakes. It

    GIST.nb 27

  • all has to be packaged up in a neat point-and-click interface ready for deployment to users a pretty typical soft-ware development task.

    Here is a detail from a typical image.

    img

    The task really breaks into three parts:

    1. Image processing

    2. An optimal GUI for interaction.

    3. Report generation

    Step 1 seems like it should be the hardest, but can be achieved in very little code. First we need to clean up theimage by deleting dirt and texture.

    DeleteSmallComponents will remove small white dots within the particles, then inverting and doing it again with a parameter lets us delete large black components up to the parame-ter size.img2 DeleteSmallComponents

    ColorNegateDeleteSmallComponentsBinarizeimg, 200

    Once cleaned we need to erode the components back to a single pixel. But we need multi-ple particles which slightly overlap to break and erode back to separate points. This ultimate erosion can be done using MaxDetect on the distance transform. We can then use ComponentMeasurements to get information on these points in this case the coordinates of the center.ComponentMeasurementsMaxDetectDistanceTransformimg2, "Centroid"

    As a final cleanup, I used DeleteDuplicates to remove spurious points that come from badly shaped particles. I give a user parameter for how close points should be before they are considered the same. Here is all of that packaged into a function.identifyBlobsimg_, min_, seperation_ : DeleteDuplicatesLast

    ComponentMeasurementsMaxDetectDistanceTransformDeleteSmallComponentsColorNegateDeleteSmallComponentsBinarizeimg, min,

    "Centroid", EuclideanDistance1, 2 seperation &

    Medical Image ProcessingHere is the entire code for that application. With the image processing, report generation, data analysis, and userinterface it amounts to only 30 lines.

    CreatePaletteDynamicModuleimg Image1, file "", data,Manipulatedata identifyBlobsimg, minSize, minSeperation;w, h ImageDimensionsimg;Showimg, GraphicsThickness.001, Cyan, Line0, h l, w, h r,

    Interfacedata, Locator, LocatorAutoCreate True, Appearance Style"", 20, White,

    Button"Import image", IfMatchQ, _Image, img &

    28 GIST.nb

  • _QuietImportfile SystemDialogInput"FileOpen", Method "Queued",

    Button"generate report", GenerateReportfile, data, h l, h r, w,

    OpenerView"auto detection settings", ColumnControlminSize, 50, "minumum particle size", 0, 200,ControlminSeperation, 7, "minumum seperation", 0, 20

    ,

    l, 0.9, "", 0, 1, ControlPlacement Left, ControlType VerticalSlider,r, 0.9, "", 0, 1, ControlPlacement Right, ControlType VerticalSlider,

    ContentSize 700, 400,

    Initialization Image recognition functionidentifyBlobsimage_, minBlob_, minSeperation_ :DeleteDuplicatesLast ComponentMeasurements

    MaxDetectDistanceTransformDeleteSmallComponentsColorNegateDeleteSmallComponentsBinarizeimage, minBlob,

    "Centroid", EuclideanDistance1, 2 minSeperation &;

    depthx_, y_, w_, l_, r_ : x, l xw r l y;

    Report generation functionGenerateReportpath_, data_, l1_, l2_, w_ : BlockpenetrationData,

    penetrationData depth, w, l1, l2 & data;

    IfLengthdata 0, CreateDocumentTextCell"Particle impact analysis", "Title",TextCellRow"Sample: ", path, "\n", "Report generated ",

    DateString, "\n", "Particles found: ", Lengthdata, "Text",ExpressionCellDistributionChartpenetrationDataAll, 1,

    PlotLabel "Spread profile", BarOrigin Left, "Output",ExpressionCellDistributionChartpenetrationDataAll, 2,

    PlotLabel "Depth profile", BarOrigin Top, "Output",ExpressionCellListLinePlotMapMean1 &,

    DeleteCasesBinListspenetrationData, w 25, 10^10, , ,PlotLabel "Mean depth", "Output",

    ExpressionCellDensityHistogrampenetrationData, 8,PlotLabel "Density", "Output",

    TextCell"Particle coordinates", "Section",ExpressionCellSortRoundpenetrationData, "Input";

    , WindowTitle "Particle analyzer"

    An Engineering Control Problem

    GIST.nb 29

  • Mathematica as solver

    An Engineering Control ProblemOur third example has its place in engineering. We will be using Mathematica to simulate an inverted pendulum youve probably seen one in a Segway. Wolframs Andrew Moylan has done some good work on this example below.

    x

    mass m

    masforce fq

    In this example well model a single inverted pendulum on a moving cart, shown here, and use the new controlsystems functionality to stabilize it.

    First we need to simulate the pendulum-and-cart system, and then we can try to determine a control force f tthat will move the cart to keep the pendulum balanced upright.

    Assuming the pendulum arm is uniform (so its center of mass is at d 2), ignoring friction, and using dimensionlessunits in which the gravitational acceleration g 1, the model pendulum-and-cart system shown above satisfies apair of differential equations.

    Lets explore the concrete case where m c d 1:eqns 2 ft Cost t2 Sint t 4 xt,

    2 Cost 2 Sint xt t 0;

    30 GIST.nb

  • Using NDSolve, Mathematicas numerical differential equation solver, we can immedi-ately simulate this system from a slightly off-center initial condition q0 p 2 - 0.1. Well initially use no control force: f t 0; that is, we make no attempt to move the cart to keep the pendulum upright.NDSolveJoineqns . ft 0, x0 x'0 '0 0, 0 2 0.1,x, , t, 0, 30

    Here is an animation of the solution. The animation code is included at the end of this notebook.AnimatePendulumFirst

    We can combine the above two steps into a handy function that animates the system for any control force f t:SimulatePendulumforce_ : AnimatePendulumFirstNDSolveJoineqns . ft force,

    x0 x'0 '0 0, 0 2 0.1, x, , t, 0, 30You can then use this SimulatePendulum function to explore ideas for keeping the pendulum balanced upright.

    The simplest plausible idea I can think of is to push the cart in whichever direction the pendulum is currently leaning move the cart under the pendulum, to stop it falling over. The pendulum is perfectly upright when qt p 2, so lets try pushing the cart with the force f t p 2 - qt:SimulatePendulum3 2 t

    GIST.nb 31

  • Maybe we can make it smoother by also taking into account how fast the pendulum is falling, which is qt: (the first derivative of q)SimulatePendulum3 2 t 't

    That stopped the wobbling nicely, but now the whole system is coasting steadily off to the right. What if we want thecart to stay at the point xt 0?We could go on modifying the control force by hand, and try to get a working solution but its much easier to turn toMathematicas new control systems design features.

    We want to keep the state of the system near the unstable equilibrium point:

    TraditionalFormxt, x't, t, 't 0, 0, 2, 0The equilibrium is unstable because, in the absence of any control force, small deviations from this point tend toget larger.

    The hand-made control forces we tried above are special cases of linear control, in which the control force is someconstant linear combination of the deviation of the states from the equilibrium point:

    TraditionalFormHoldFormft k1 xt k2 xt k3 t 2 k4 t

    The constants ki are called feedback gains, and we can use a couple of new control theory functions in Mathemat-ica 8 to find good values for them.

    An Engineering Control ProblemWe begin by constructing a linear model for the behavior of the system near the equilibrium point. The functionStateSpaceModel does this automatically if we give it the nonlinear equations:

    32 GIST.nb

  • model StateSpaceModeleqns,xt, 0, x't, 0, t, 2 , 't, 0, ft, , t

    In the third argument to StateSpaceModel, we specified that there is one control variable: f t.The result of StateSpaceModel looks like an augmented matrix. Its columns describe the effect of small devia-tions of each of the state variables xt, xt, qt, qt from equilibrium. For example, the third column says that,near the equilibrium point, small deviations in qt away from the upright position (qt p 2) lead to an increasein both x ' t (the speed of the cart) and qt (the rate at which the pendulum is falling). The augmented fifthcolumn similarly describes the immediate effect of the control force on the state variables.

    Now that we have a model for the system, we can use the function LQRegulatorGains to find values for the feedback gains ki:gains LQRegulatorGainsNmodel, DiagonalMatrix1, 10, 10, 100, 1 First

    In the second argument of LQRegulatorGains, we specified the following quadratic cost function for thesystem:

    TraditionalFormHoldFormcost 1 xt2 10 xt2 10 t2 100 t2 1 ft^2LQRegulatorGains finds values for the constants ki that minimize this cost function over time (in the linearizedmodel).

    The cost coefficients 1, 10, 10, 100 are a guess, based on an intuition that deviations in qt are relatively moreimportant than deviations in xt and qt, which are in turn more important than deviations in xt. Differentcoefficients would give better, worse, or just qualitatively different behavior. In real life the coefficients mightrepresent actual costs associated with deviations of a system from the desired state.

    Using these gains, our control force is this :controlforce gains.xt, x't, t 2 , 't

    Lets test it out:SimulatePendulumcontrolforce

    An Engineering Control ProblemOur pendulum stabilized via LQRegulatorGains can survive some quite bumpy conditions:

    Generate random bumpy conditions:verybumpyt_ SumRandomReal10, 10 Exp10 t RandomInteger152, 20;

    GIST.nb 33

  • Specific verybumpy used in blog post:verybumpyt_ 13.203576835315666` 10 15t2 1.4310438601615516` 10 12t2

    2.3670864940973857` 10 11t2 7.791914458883305` 10 9t2 12.661390107806994` 10 8t2 13.39019250013871` 10 7t2 7.873272031334967` 10 6t2 6.707528013930826` 10 2t2 2.548183565506953` 10 1t2 7.231209749707311` 10 t2;

    Plotverybumpyt, t, 0, 30, PlotRange All,Filling Axis, PlotLabel HoldFormverybumpyt, AxesLabel t

    SimulatePendulumcontrolforce verybumpyt

    If you bump it hard enough, the linear control system we have used here wont be able to recover. You can try thatout by downloading the CDF file for this talk. Another natural thing to explore would be the effect of different costfunctions in LQRegulatorGains. Its also fun to try to design your own superior control force by hand for thisrelatively simple system.

    The Statistics of Wind Power

    Image courtesy of Wikipedia: User Fanny Schertzer

    The Statistics of Wind PowerWhat can Mathematica contribute in the field of sustainable energy? It turns out with a combination of Wolframsbuilt-in data and our newly expanded statistical functionality, Mathematica can make quite a bit of headway.

    34 GIST.nb

  • In this example we use information from WeatherData and investigate the feasibility of using wind to generateelectricity for central Illinois. [We will also make use of external data from (www.vestas.com/en/wind-power-plants/procurement/turbine-overview/v90-1.8/2.0-mw.aspx#/vestas-univers) in our analysis.]

    First we will get the average daily wind speed for the weather station nearest to Bloom-ington IL, beginning in the year 2000.wsData WeatherData"Bloomington", "Illinois",

    "MeanWindSpeed", 2000, 1, 1, Date, "Day"

    We can take a look at the form of the data and visualize the data in the time series immediately:DateListPlotwsData, Joined TrueThe entries are ordered pairs consisting of the date and the average wind speed for the day. We will need to extractthe speeds from the data.

    I will take the second part of all the data points and convert from km/hr to m/s. Now lets visualize the histogram quickly.wndData 1000 wsDataAll, 2 602;histplot HistogramwndData, 45, "PDF"

    The Statistics of Wind PowerSince wind speed can be assumed to vary continuously, we can represent the distribution for our observations usingthe SmoothKernelDistribution function.

    Visualize this distribution together with the original histogram.dist SmoothKernelDistributionwndData;Showhistplot, estplot PlotPDFdist, x, x, 0, 13, PlotStyle Thick, Red

    And we can ask questions of that distribution. For instance, what is the probability of wind speed being greater than 3.5 m/s?Probabilityspeed 3.5, speed dist

    The Statistics of Wind PowerHowever, I am going to assume that this is really the ExtremeValueDistribution, and I am going to use the data toestimate the parameters of the distribution.

    params FindDistributionParameterswndData, ExtremeValueDistribution, distEV ExtremeValueDistribution, . params

    And now lets test to see if our assumption should be rejected.tstData DistributionFitTestwndData, distEV, "HypothesisTestData";tstData"AutomaticTest", tstData"TestConclusion"

    Here is the test statistic and p-value for the test.tstData"TestDataTable"

    We can now visualize our distribution estimate.Showhistplot, estplot PlotPDFdistEV, x, x, 0, 13, PlotStyle Thick, Red

    GIST.nb 35

  • The generator we are using for our analysis here requires 3.5 m/s wind speed to generate electricity. We can use theProbability function to estimate what proportion of time the generator will be contributing to the grid.

    Based on the data distribution, we can compute the probability that wind speed is greater than the threshold of 3.5 m/s:Probabilityx 3.5, x distEV

    Lets visualize the result.PlotEvaluatePDFdistEV, x 1, Boolex 3.5, x, 0, 13, Filling 2 Axis

    The Statistics of Wind PowerFrom the manufacturers spec sheet for the V90-1.8/2.0 MW generator we can make a chart indicating anticipatedperformance levels in the central Illinois location.

    GridStyle"Wind Speed ms", FontWeight "Bold", Style"Probability",

    FontWeight "Bold", Style"Estimated Power MW", FontWeight "Bold"," 3.5", Probabilityx 3.5, x distEV, 0.0,"Between 3.5 and 7", Probability3.5 x 7, x distEV, "0 to .8","Between 7 and 13", Probability7 x 13, x distEV, ".8 to 1.65"," 13", Probability13 x, x distEV, "1.65"

    , BaseStyle "Menu", 19, Alignment Left,Background LightYellow, Frame False, All, Spacings 2, 0.62

    We can calculate the power that this generator will accumulate over the time period from 2000 until now:power Total

    Probability3.5 x 7, x distEV Mean0, 0.8,Probability7 x 13, x distEV Mean1.65, 0.8,Probability13 x, x distEV 1.65

    hours in a yearResult

    cost of electricity in ILResult

    If we were to purchase electricity for that much power, we would have spent the follow-ing amount for one year. Keep in mind, this is only for one wind generator.valuePerYear power 8760 12.4 100 1000

    Website Analysis

    Combining data import, transformation and graph theory

    If you are curious about the structure of a website, Mathematica can help. Lets create a webcrawler to find linkagesbetween a specified URL and all of its hyperlinks:

    36 GIST.nb

  • webcrawlerrooturl_, depth_ : FlattenRestNestListUnionFlattenThread Import, "Hyperlinks" & Last &,

    "" rooturl, depth;

    Pick a website of interest, say twitter:twdata Quietwebcrawler"http:www.twitter.com", 2;

    Visualize the result by generating a graph of networks. Mouse over the vertices to see the names of the nodes.g Graphtwdata, VertexLabels Placed"Name", Tooltip

    Website AnalysisUsing the HITSCentrality function, find the HITS hub and authority centralities for the graph.

    MatrixFormHITSCentralityg

    Link the pages with their HITS centralities.pagesHITS TransposeVertexListg, HITSCentralityg1;TableFormpagesHITS1 ;; 5

    We need to sort the pages according to their degree centralities. Here is the top 10 list.sortedpages SortpagesHITS, 12 22 &;TableFormsortedpages1 ;; 10

    This shows the subgraph containing all of the top pages interactions with other pages hyperlinked from twitter.com.HighlightGraphg, NeighborhoodGraphg, sortedpages2, 1

    Filtering Sound from Apollo 11

    Signal processing with wavelets

    Wavelet analysis is one of those topics which has a broad range of applications. We can use wavelets for imagecompression, for signal detection in time series data, etc. Wavelet analysis is now an integrated part of Mathemat-ica. In this example, Ill demonstrate how wavelet analysis can be applied to sound data.

    Here is a sound file from NASAs Apollo 11 mission, the spaceflight that landed the first humans on the moon in 1969. snd ExampleData"Sound", "Apollo11ReturnSafely"

    Perform a discrete wavelet transform on this sound data directly.dwd DiscreteWaveletTransformsnd, CDFWaveletWaveletListPlotdwd

    GIST.nb 37

  • 5000 10 000 15 000 20 000 25 000

    151413121110

    987654321

    In this case, we would like to decrease the backgroungmad noise level by performing a wavelet threshold on the data.thr WaveletThresholddwd, "Soft", .05;

    How does it sound?InverseWaveletTransformthrNow it sounds much better!

    Fun with Mazes

    Combining image processing, solving, visualization, and interfaces

    Fun with MazesLast but not least, I would like to show you a recreational example of solving a maze with Mathematica.

    maze ;

    highlightMazePathpath_ :ShowGraphicsGrayLevel0.2, Rectangle1., 1., 30., 20.,

    maze, Graphpath, EdgeListPathGraphpath, VertexCoordinates AbsoluteOptionsmaze, VertexCoordinates2, VertexIndexmaze, & path,

    VertexSize 0, EdgeStyle DirectiveThickness0.01, Red,Opacity1, CapForm"Round", ImageSize 500

    38 GIST.nb

  • Note: the following Export command will take a considerable amount of time to run depending on your computer system.Export"maze.mov", Table

    highlightMazePathFindShortestPathmaze, "in", "out"1 ;; n, n, 2, 307, 1SystemOpen"maze.mov"

    We can also use Manipulate to see how the maze is solved.ManipulatehighlightMazePathFindShortestPathmaze, "in", "out"1 ;; n,n, 2, 307, 1, SaveDefinitions True

    GIST.nb 39

  • Mathematica Tech TalkParallel Computation with Mathematica

    Parallel Computation Architecture in MathematicaMathematica provides a high-level, parallel language interface that allows you to connect multiple Mathematicakernels using the MathLink communication protocol and provides a framework to distribute calculations andcollect results.

    Parallelism in Mathematica: Extending the functionality of Mathematica

    The kernel is the part of Mathematica that performs computations. Every remote kernel contains the full functional-ity of a standard Mathematica kernel.

    The Mathematica kernel contains:

    Language: a consistent high-level language; choose the programming paradigm that is best for your

    computation needs procedural, functional, rule-based, recursive, or object-oriented.

    Mathematics & algorithms: wide range of functions for mathematics, science, engineering; automatic

    algorithm selection; state-of-the -art performance, scalability.

    Data handling: automatic integration of hundreds of data formats; load-on-demand, curated data in math,

    physics, chemistry, finance, and geography.

    System interfaces & deployment: databases, spreadsheets, legacy C or Fortran code, Web Services, Java,

    .NET

    The managing computer can use the Mathematica front end for:

    Visualization & graphics: automatic creation of high-impact 2D and 3D visualizations of functions and data.

    Dynamic interactivity: create dynamic interfaces quickly and easily; manipulate graphics, math, tables, text,

    etc.

    Notebooks & documents: integrated technical word processor; write documents with typeset mathematical

    formulae; output to PDF, HTML, XML; create slide show presentations.

    Parallelism: built-in parallel efficiency visualization tools in Mathematica

    Getting Started

    40 GIST.nb

  • In the Evaluation menu, you can find the status of all your currently running kernels, as well as configure new ones,through the Parallel Kernel Status and Parallel Kernel Configuration menu items, respectively.Additionally, you can use the following commands to programmatically work with kernels:

    The LaunchKernels command will explicitly launch all kernels that you have configured on your system.LaunchKernels

    You can see a list of the running kernels via the Kernels command.Kernels

    You can get the number of running kernels via $KernelCount.$KernelCount

    You can shut down the kernels with the CloseKernels command.CloseKernels

    Root Finding In this example, we want to find solutions to the equation cost2 = -t + 0.3 using Newtons method. The Mathemat-ica function FindRoot can be used for this. FindRoot takes as its input the equation and a starting point for thealgorithm. By using multiple starting points, we can find all roots of the equations in the domain.

    plot PlotCost2, Expt .3, t, 0, 3

    Create some starting points for Newtons method.startingvals Tablex, x, 0, 3 , .1;vals MapFindRootCost2 Expt .3, t, 1 &, startingvals; AbsoluteTiming

    Plot the two curves along with the solutions.Showplot, GraphicsRed, PointSizeMedium,

    PointMapThreadt . 1, 2 &, vals, Cost2 . vals

    Root Finding: Root finding in parallelHere is a parallel approach to the problem, achieved by replacing Map with ParallelMap.

    vals FindRootCost2 Expt .3, t, 1 & startingvals; AbsoluteTiming

    pvals ParallelMapFindRootCost2 Expt .3, t, 1 &, startingvals; AbsoluteTiming

    Showplot, GraphicsRed, PointSizeMedium,PointMapThreadt . 1, 2 &, pvals, Cost2 . pvals

    Root Finding: Root finding in parallel behind the scenes

    GIST.nb 41

  • We can use the debug tools to get a better look at what is happening when we use ParallelMap and to look at theefficiency of our parallelization strategy.

    SetOptions$Parallel, Tracers SendReceiveSetOptions$Parallel, TraceHandler "Save"

    Notice that ParallelMap splits the starting values between the worker kernels. Also notice that some of the worker kernels have found the same roots.pvals ParallelMapFindRootCost2 Expt .3, t, 1 &, startingvals; AbsoluteTiming

    TableFormTraceList, TableDepth 2,TableHeadings Automatic, "Trigger", "Event"

    Turn off debugging:SetOptions$Parallel, Tracers SetOptions$Parallel, TraceHandler "Print"

    Monte Carlo: Calculating pThe ratio of the number of random points generated inside a square that also fall within an inscribed circle can berelated to p.Solvepi r2 PointsInsideTotalPoints , pi

    Generate some points and then determine if they are inside or outside the circle.

    r 12 ;pts RandomRealr, r, 5000, 2;insidepts Selectpts, Norm1 r &Graphics

    EdgeFormThick, White, Rectangle.5, .5,Black, Circle0, 0, .5,Red, Pointinsidepts,Black, PointComplementpts, insidepts

    Apply the formula, but we get a bad approximation.

    N LengthinsideptsLengthpts r2

    Use more points.pts RandomRealr, r, 50 000, 2;insidepts Selectpts, Norm1 r &;Lengthinsidepts Lengthpts r^2 NWe can keep adding points to get better approximations, but it will continue to get slower, as well.

    Monte Carlo: Calculating p in parallelSo lets try to do this in parallel!

    42 GIST.nb

  • DistributeDefinitionsrpts ParallelTableRandomRealr, r, 25 000, 2, LengthKernels;insidepts ParallelMapSelect, Norm r & &, pts;

    The different colors represent different kernels. Each kernel has a unique set of random points.Withlen Lengthpts,MapIndexedGraphics

    EdgeFormThick, White, Rectangler, r,Black, Circle0, 0, r,Hue21 len, PointSizeSmall, Point11,LighterHue21 len, .5, PointComplement11, 12,

    ImageSize 225 &, Transposepts, insidepts

    Combine results and calculate p!DistributeDefinitionspts, insidepts; NMeanParallelTable

    Lengthinsideptsi Lengthptsi r^2, i, LengthKernels

    We can optimize our routine a little bit and compute p using 10 million points. AbsoluteTimingWith NUMPOINTS 10 000 000, CPUS LengthKernels,

    partitions QuotientNUMPOINTS, CPUS JoinTable1, ModNUMPOINTS, CPUS, Table0, CPUS ModNUMPOINTS, CPUS;

    NTotalParallelMap TotalRoundTotalRandomReal1 2, 1 2, , 2^2,2 .25 &, partitions NUMPOINTS 1 1 2^2

    Monte Carlo: Calculating p benchmarksLets perform some benchmarks. Note that the cells are unevaluatable because they can take a long time to run. Ifyou would like to reproduce these results simply make the cells evaluatable (from the Cell menu, select Cell Proper-ties Evaluatable).maxpoints Range10 000 000, 80 000 000, 1000 000;pts TableAbsoluteTimingWith NUMPOINTS max, CPUS LengthKernels,

    partitions QuotientNUMPOINTS, CPUS JoinTable1,ModNUMPOINTS, CPUS, Table0, CPUS ModNUMPOINTS, CPUS;

    NTotalParallelMap TotalRoundTotalRandomReal1 2, 1 2, , 2^2, 2 .25 &,

    partitions NUMPOINTS 1 1 2^2, max, maxpoints;

    GIST.nb 43

  • ListLinePlotMapThread1, 2 &, maxpoints, ptsAll, 1, Frame True,FrameLabel Style"Number of Points", 12, Style"Seconds", 12

    1107 2107 3 107 4 107 5 107 6 107 7107 81075

    10

    15

    20

    25

    Number of Points

    Seco

    nds

    Visualizing the Mandelbrot SetIn this example we will take a look at how to visualize the Mandelbrot set.

    First we define a function that returns the number of times it took for the set to con-verge at a given (x, y) coordinate.MandelbrotFunction Compilec, _Complex,

    LengthFixedPointList2 c &, c, 50, SameTest Abs2 2.0 &

    Compute the data using Table.AbsoluteTimingdataSerial

    TableMandelbrotFunctionx y , x, 2, 0.5, 0.002, y, 1, 1, 0.002;

    We can use ArrayPlot to visualize the data.ArrayPlotTransposedataSerial, ColorFunction Hue, ImageSize 800 Timing

    Visualizing the Mandelbrot Set: In parallelTo parallelize the process, Table can be replaced with ParallelTable as in the previous examples.

    DistributeDefinitionsMandelbrotFunction;AbsoluteTimingdataParallel ParallelTable

    MandelbrotFunctionx y , x, 2, 0.5, 0.002, y, 1, 1, 0.002;

    The parallel functions in Mathematica support a Method option, that adjusts load bal-ancing. The automatic setting results in medium load balancing and is usually the best choice. This option is documented in the reference page for Parallelize.DistributeDefinitionsMandelbrotFunction;AbsoluteTimingdataParallelC ParallelTableMandelbrotFunctionx y , x, 2, 0.5, 0.002,

    y, 1, 1, 0.002, Method "CoarsestGrained";DistributeDefinitionsMandelbrotFunction;AbsoluteTimingdataParallelF ParallelTableMandelbrotFunctionx y , x, 2, 0.5, 0.002,

    y, 1, 1, 0.002, Method "FinestGrained";

    44 GIST.nb

  • We can use ArrayPlot to visualize the data and check to see if we obtained the same results.ArrayPlotTransposedataParallel, ColorFunction Hue, ImageSize 800

    Binomial Stock Options: Problem setupUse the Binomial model to predict the value of a stock option at the time of maturity.

    Helper functions

    Visualization function

    Data structure

    Binomial Stock Options: Finding valuationsUse the Binomial model to predict the value of a stock option at the time of maturity.

    Find volatilities and tables of strike values for Microsoft and Google.msftvolatility

    StandardDeviationFinancialData"msft", "Jan. 1, 2008" All, 2 FinancialData"msft", "Jan. 1, 2008"1, 2;

    googvolatility StandardDeviationFinancialData"goog", "Jan. 1, 2008" All, 2 FinancialData"goog", "Jan. 1, 2008"1, 2;

    msftstrikevalues Tablex, x, 10, 50, 2.5googstrikevalues Tablex, x, 270, 660, 10;

    Try the function out on calls and puts for Microsoft across the entire strike price range.msftoptionsprices MapReplacePartbinomialprice28.69, msftvolatility, 5 252, .06869,

    5 365, 11, , callvalue1, 1 ,ReplacePartbinomialprice28.69, msftvolatility, 5 252, .06869, 5 365,

    11, , putvalue1, 1 &, msftstrikevalues AbsoluteTiming

    To run this in parallel, the worker kernels must have all of the function definitions, not just the ones explicitly specified.DistributeDefinitionsbinomialprice, up, down, oneup, f,msftvolatility, googvolatility, probabilityup, callvalue, putvalue

    msftoptionsprices ParallelMapReplacePartbinomialprice28.69, msftvolatility, 5 252,

    .06869, 5 365, 11, , callvalue1, 1 ,ReplacePartbinomialprice28.69, msftvolatility, 5 252, .06869, 5 365,

    11, , putvalue1, 1 &, msftstrikevalues AbsoluteTiming

    Now well do calls and puts for Google over a much larger range of strike values:googoptionsprices

    ParallelMapReplacePartbinomialprice540.3, googvolatility, 5 252,.06869, 5 365, 14, , callvalue1, 1 ,

    ReplacePartbinomialprice540.3, googvolatility, 5 252, .06869, 5 365,14, , putvalue1, 1 &, googstrikevalues; AbsoluteTiming

    GIST.nb 45

  • ListLinePlotgoogoptionspricesAll, 112 ;; 31,Frame True, FrameLabel "Strike", "Price",PlotLabel "Market vs. Binomial price GOOG CALLs"

    ListLinePlotgoogoptionspricesAll, 212 ;; 31,Frame True, FrameLabel "Strike", "Price",PlotLabel "Market vs. Binomial price GOOG PUTs"

    Genomic Data: Comparing organismsIn this example we try to determine which organism is closer genetically to another organism. We do this by comput-ing edit distances between genes in the target organism to the two candidate organisms.

    Import the FASTA files:picrogenes Import"picrophilus_torridus_gene.fna";acidogenes Import"thermoplasma_acidophilum_gene.fna";volcaniumgenes Import"thermoplasma_volcanium_gene.fna";

    Load gene data on the remote nodes.Withpicrogenes picrogenes, acidogenes acidogenes,

    volcaniumgenes volcaniumgenes, ParallelEvaluatepicro picrogenes; acido acidogenes; volcanium volcaniumgenes;;;

    This is what one of the genes on the first worker kernel looks like.ParallelEvaluatepicro1, Kernels1Now that we have the data on the remote nodes, we can do some work on it.

    Genomic Data: Comparing organismsCompare the first eight genes of thermoplasma volcanium to thermoplasma acidophilum and return the minimumedit distance between the gene and the genes in acidophilum.

    acidovolcanium ParallelMapFunctiongene, MinEditDistancegene, & acido,volcaniumgenes1 ;; 8; AbsoluteTiming

    acidovolcanium

    Do the same for the first eight genes in thermoplasma volcanium and picrophilus torridus.picrovolcanium

    ParallelMapFunctiongene, MinEditDistancegene, & picro,volcaniumgenes1 ;; 8; AbsoluteTiming

    Compute the mean edit distance for the eight genes between acidophilum and volca-nium vs. torridus and volcanium.Meanacidovolcanium NMeanpicrovolcanium NListLinePlotacidovolcanium, picrovolcanium

    46 GIST.nb

  • Genomic Data; Comparing organisms vs. actual scientific classification

    Lets take a look at the respective positions in the scientific classification of the compared organisms.

    tree Archaea Euryarchaeota,Euryarchaeota Thermoplasmata, Thermoplasmata Thermosplasmatales,Thermosplasmatales Thermosplasmataceae,Thermosplasmataceae Thermoplasma, Thermoplasma acidophilum,Thermoplasma volcanium, Thermosplasmatales Picrophilaceae,Picrophilaceae Picrophilus, Picrophilus torridus;

    TreePlottree, Automatic, Archaea, VertexLabeling True

    Genomic Data: Comparing organisms benchmarksSome benchmarking, first using ParallelMap to compute the minimum edit distances for genes 18, 110, 112, etc.

    acidovolcaniumtiming TableAbsoluteTimingParallelMapFunctiongene, MinEditDistancegene, & acido,volcaniumgenes1 ;; x, x, 8, 56, 2;

    The number of gene computations vs. time to compute. Notice that there are some step-wise properties.ListLinePlotMapThread1, 2 &, Tablex, x, 8, 56, 2, acidovolcaniumtimingAll, 1,Frame True, FrameLabel "Number of Comparisons", "Seconds"

    10 20 30 40 5050

    100

    150

    200

    Number of Comparisons

    Seco

    nds

    For our example, it is not the case that the computations will all be equally difficult. Comparing gene 2 and compar-ing gene 8 might be very different in terms of computation time if gene 2 is relatively short and gene 8 is relativelylong.

    Genomic Data: Comparing organisms using ParallelSubmit and WaitAll

    This example may be better suited to using ParallelSubmit and WaitAll.

    pids MapParallelSubmitf; Pause1 &, Tabley, y, 16WaitAllpids

    GIST.nb 47

  • Before, it took a combined 100 seconds to compute this.acidovolcanium

    MapFunctiongene, ParallelSubmitMinEditDistancegene, & acido,volcaniumgenes1 ;; 8;

    picrovolcanium MapFunctiongene, ParallelSubmitMinEditDistancegene, & picro, volcaniumgenes1 ;; 8;

    acidovolcanium;picrovolcanium;acidovolcaniumresult WaitAllacidovolcanium;picrovolcaniumresult WaitAllpicrovolcanium; AbsoluteTiming

    Of course its important we get the same results!Meanacidovolcaniumresult NMeanpicrovolcaniumresult N

    Genomic Data: Comparing organisms benchmarking ParallelSubmit and WaitAll

    Again some benchmarking.

    acidovolcaniumtiming TableAbsoluteTimingpids MapFunctiongene, ParallelSubmitMinEditDistancegene, & acido,volcaniumgenes1 ;; x; WaitAllpids;, x, 8, 56, 2;

    ListLinePlotMapThread1, 2 &, Tablex, x, 8, 56, 2, acidovolcaniumtimingAll, 1,Frame True, FrameLabel "Number of Comparisons", "Seconds"

    10 20 30 40 5050

    100

    150

    200

    Number of Comparisons

    Seco

    nds

    Airfoil Wing DesignNow lets have a look at how we can usefully apply parallelization to existing code for a real-world problem.

    The example we are going to look at comes from the field of aeronautics, in the area of airfoil design. Using somesimple code adapted from the materials presented in Foundations of Aerodynamics: Bases of Aerodynamic Design5th edition by Arnold M. Kuethe and Chuen-Yen Chow, well see how to spot areas where parallelization can beusefully applied and implemented.

    Airfoil dataAirfoils are commonly constructed from datasets. The example considered here is the NACA 64A010 airfoil and thesource of the dataset is the Pubic Domain Aeronautical Software website: www.ae.uiuc.edu/m-selig/ads/coord/-naca64a010.dat (clicking the link will download the data to your computer).

    We begin by importing the dataset to Mathematica.

    48 GIST.nb

  • Import the data and give it a name.predata Import"naca64a010.dat", "Table";The dataset is small enough that we can look at it to determine its structure, but it is generally a good idea to getsome sense of the size before trying to view the data.

    Determining the size of the dataset.DimensionspredataThe single value indicates there are 112 rows in the data and they are not all of equal length. In this case we can seethat we have uniform rows if we drop the first row.

    The dimensions of the data with the first row deleted.DimensionsRestpredata

    Visualizing the data using ListLinePlot.ListLinePlotRestpredata, Mesh All, AspectRatio Automatic

    Airfoil Wing Design: Deriving panel pointsThis particular dataset gives the coordinates, starting at the trailing edge of the top surface, proceeding around thecontour until it reaches the trailing edge of the bottom surface. These coordinates define the basic panels of theairfoil.

    However, what you usually want to do is look at an arbitrary number of panels. For this we need to take thesepoints and parameterize the airfoil so that we can use coordinates of our choosing.

    ParameterizeAirfoilarry_?MatrixQ :Moduleindxed MapIndexedJoin2, 1 &, arry, xfun, yfun,xfun InterpolationindxedAll, ;; 2, Method "Spline";yfun InterpolationindxedAll, 1 ;; 3 ;; 2, Method "Spline";xfun, yfun

    nacaChrd, nacaCam ParameterizeAirfoilRestpredata

    Visualizing the parametrization. Note the parametrization traverses the upper camber first.ManipulateShowParametricPlotnacaChrdt, nacaCamt,

    t, 1, 35, PlotRangePadding Scaled.03, GraphicsRed, PointSizeLarge, PointnacaChrdi, nacaCami, i, 1, 35

    The next step in the process is to create boundary points for the panels we will use. MakePointSetlis1_ ; VectorQlis1, NumericQ, lis2_ ; VectorQlis2, IntegerQ ;

    Lengthlis1 Lengthlis2 1 :Modulediffs Differenceslis1, stps, len, pts, stps diffs lis2;len Lengthdiffs;pts SortJoinFlattenTablej, i, 1, len, j, lis1i,

    lis1i diffsi stpsi, stpsi, 1, lis11;JoinReversepts, Restpts

    PanelPointsfn1_InterpolatingFunction, fn2_InterpolatingFunction, ptslis_List :

    ReverseWithmx fn11, 1, 2, len Lengthptslis, TableIfi Floorlen 2,fn1t, fn2t . FindRootfn1t ptslisi, t, mx 4, fn1t,

    fn2t . QuietFindRootfn1t ptslisi, t, 3 mx 4, i, len

    GIST.nb 49

  • Creating the point set for the x coordinates. Here we establish several region of differing step sizes.pts MakePointSet0, .05, .13, .18, .75, 1., 20, 14, 9, 6, 5;

    Next we create the panel boundaries. ShortpnlPts PanelPointsnacaChrd, nacaCam, pts, 6

    Visualizing the pnlPts set.GraphicsGray, PolygonpnlPts

    Airfoil Wing Design: Airfoil functionsThe calculations we will be examining are the pressure coefficients over each wing panel as defined by our airfoildata. To do this we will first define a number of functions which will be used in the function PressureCoefficientsSe-rial. The original code was in FORTRAN but we have rewritten it here in Mathematica.

    PressureCoefficientsSerial takes the points defining the panels as well as an angle of attack for the airflow andreturns a list containing panel midpoints, panel angles, panel lengths, velocity vector and pressure coefficientvector for a 2D panel method for an airfoil defined by the points at the angle of attack.

    First we must define a large set of subfunctions.

    Functions for calculating normal velocity coefficients.CNorm1i_, j_ :Whichi j, 1, True, DDi, j FFi, j 2 CCi, j GGi, j CNorm2i, j

    CNorm2i_, j_ : Whichi j, 1, True, DDi, j QQi, j FFi, j 2 Sj AAi, j CCi, j DDi, j EEi, j GGi, j Sj

    ANormi_, j_, m_ : Whichi m 1 && j 1, 1, i m 1 && j m 1, 1, i m 1, 0,j 1, CNorm1i, 1, j m 1, CNorm2i, m, True, CNorm1i, j CNorm2i, j 1

    50 GIST.nb

  • Auxiliary functions for the 2D panel methods. These are taken directly from Founda-tions of Aerodynamics by Arnold M. Keuthe and Chuen-Yen Chow with adjustments to take advantage of built-in Mathematica functions.AAi_, j_ : xi XBj Costhetaj yi YBj Sinthetaj

    BBi_, j_ : SquaredEuclideanDistancexi, yi, XBj, YBj

    CCi_, j_ : Sinthetai thetaj

    DDi_, j_ : Costhetai thetaj

    EEi_, j_ : xi XBj Sinthetaj yi YBj Costhetaj

    FFi_, j_ : Log1 Sj^2 2 AAi, j Sj BBi, j

    GGi_, j_ : ArcTanBBi, j AAi, j Sj, EEi, j Sj

    PPi_, j_ :xi XBj Sinthetai 2 thetaj yi YBj Costhetai 2 thetaj

    QQi_, j_ :xi XBj Costhetai 2 thetaj yi YBj Sinthetai 2 thetaj

    Functions for calculating tangential velocity coefficients.CTan1i_, j_ :Whichi j, 2, True, CCi, j FFi, j 2 DDi, j GGi, j CTan2i, j

    CTan2i_, j_ : Whichi j, 2, True, CCi, j PPi, j FFi, j 2 Sj AAi, j DDi, j CCi, j EEi, j GGi, j Sj

    ATani_, j_, m_ :Whichj 1, CTan1i, 1, j m 1, CTan2i, m, True, CTan1i, j CTan2i, j 1

    The above functions require definitions for 6 variables x, y, XB, YB, S, and theta. CreateVarSets defines the appropriate variable sets.CreateVarSetslis_ ; MatchQDimensionslis, _, 2 :Clearx, y, XB, YB, S, theta;len Lengthlis;Withvarlis TableXBi, YBi, i, len, varlis lis;Withvarlis Tablexi, yi, i, len 1,varlis MovingAveragelis, 2;

    Withvarlis TableSi, i, len 1,varlis ApplyEuclideanDistance, Partitionlis, 2, 1, 1;

    Withvarlis Tablethetai, i, len 1,varlis Partitionlis, 2, 1 . a_List, b_List ArcTan b a;

    An important point to note here is that the functions above, being simple functions, are not parallelizable. Hence inour analysis we can disregard them when looking for parallelization time savings. CreateVarSets could beparallelized but since we will need those definitions established on all subkernels this is also not useful to attemptto parallelize.

    GIST.nb 51

  • Finally we combine all of these functions to calculate the pressure coefficients.PressureCoefficientsSerialpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ :Modulelen LengthpnlPts 1, normcoeffs, tancoeffs, rhs, solvec,

    cosVec, VelVec, cntrlpts, pnllens, pnlangs, CreateVarSetspnlPts;normcoeffs TableANormi, j, len, i, len 1, j, len 1;rhs JoinTableSinthetai , i, len, 0;solvec LinearSolvenormcoeffs, rhs;tancoeffs TableATani, j, len, i, len, j, len 1;cosVec TableCosthetai , i, len;VelVec cosVec Totaltancoeffs.solvec, 2;cntrlpts Tablexi, yi, i, len;pnllens TableSi, i, len;pnlangs Tablethetai, i, len;Clearx, y, XB, YB, S, theta; Printsolvec;Threadcntrlpts, pnlangs, pnllens, VelVec, 1 VelVec^2

    Airfoil Wing Design: Airfoil pressure coefficients serial calculation & visualization

    Now we are ready to run the analysis in serial. We assume a 6 angle of attack. This will take a couple seconds torun.

    pressureCoefficients PressureCoefficientsSerialpnlPts, 6 ; AbsoluteTiming

    Here are 12 rows of our result.DisplayTableSortpressureCoefficients ;; 12, ColumnHeadings "Panel midpoints", "Panel angle", "Panel lengths", "Velocity", "Pressure"

    To get a visualization the pressure coefficient vectors directly we can define a function called PDVectorPlotfunction.

    Helper functions to PDVectorPlot.normgenx1_, y1_, x2_, y2_, scl_ :Modulemnvec Meanx1, y1, x2, y2, nrm, nrmvec,nrm Normalize1, x2 x1 y1 y2;Whichx1 x2 && y2 y1, nrmvec mnvec, mnvec scl nrm,x1 x2 && y1 y2, nrmvec mnvec, mnvec scl nrm,x1 x2 && y2 y1, nrmvec mnvec, mnvec scl nrm,x1 x2 && y1 y2, nrmvec mnvec, mnvec scl nrm

    SurfaceNormalslis_List, scls_List :MapThreadnormgen1, 2 &, Partitionlis, 2, 1, scls

    PDVectorPlotpnlpts_, pres_, ang_, opts : OptionsPattern :GraphicsArrowheadsSmall, Arrow SurfaceNormalspnlpts, Abspres 2,

    Polygonpnlpts, Red, ArrowheadsMedium,ArrowRotationTransformang1, 0, 1 4, 0, opts

    Visualizing the pressure coefficient vectors:PDVectorPlotpnlPts, pressureCoefficientsAll, 1, 6 , ImageSize Large

    52 GIST.nb

  • Airfoil Wing Design: Pressure coefficients analysis of time usageThere are a number of methods that can be used to determine time usage inside your code. If you have WolframWorkbench, perhaps the best method is the Workbench Profiler function. This allows you to run a computation andget a report on that calculation. The report tells you how many times each subpart of the computation was executedand how long each of these took.

    For this example I will just define a function to get the relevant information we need to find the bottlenecks inside our code.timingfunc_, tag_ : PrintNumberFormFirstAbsoluteTimingfunc, 10, tagSetAttributestiming, HoldAll;

    Next we wrap timing around the relevant calculations within our function. As we cant parallelize LinearSolve, Total, or Thread, we can omit them in our study.PressureCoefficientsDiagpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ :Modulelen LengthpnlPts 1, normcoeffs, tancoeffs, rhs, solvec,

    cosVec, VelVec, cntrlpts, pnllens, pnlangs, CreateVarSetspnlPts;timingnormcoeffs TableANormi, j, len, i, len 1, j, len 1;,"normcoeffs";

    timingrhs JoinTableSinthetai , i, len, 0, "rhs";solvec LinearSolvenormcoeffs, rhs;timingtancoeffs TableATani, j, len, i, len, j, len 1, "tancoeffs";timingcosVec TableCosthetai , i, len, "cosVec";VelVec cosVec Totaltancoeffs.solvec, 2;timingcntrlpts Tablexi, yi, i, len, "cntrlpts";timingpnllens TableSi, i, len, "pnllens";timingpnlangs Tablethetai, i, len, "pnlangs";Clearx, y, XB, YB, S, theta;Threadcntrlpts, pnlangs, pnllens, VelVec, 1 VelVec^2

    PressureCoefficientsDiagpnlPts, 6 ;

    Airfoil Wing Design: Parallelizing pressure coefficientsHaving found the two bottlenecks in our calculation, we can parallelize the Table commands involved. We also needto ensure that the arrays x, y, XB, YB, S, and theta are established and distributed. To speed things along we willalso preemptively distribute the definitions of ANorm and ATan. This also ensures that our code is compatible withMathematica 7.

    GIST.nb 53

  • PressureCoefficientsParallelpnlPts_ ; MatchQDimensionspnlPts, _, 2, _ :Modulelen LengthpnlPts 1, len1 LengthpnlPts, normcoeffs,

    tancoeffs, rhs, solvec, cosVec, VelVec, cntrlpts, pnllens, pnlangs,CreateVarSetspnlPts; DistributeDefinitionsx, y, XB, YB, S, theta,ANorm, CNorm1, CNorm2, AA, CC, DD, EE, ATan, CTan1, CTan2, PP, QQ, len1;

    normcoeffs ParallelTableANormi, j, len, i, len1, j, len1;rhs JoinTableSinthetai , i, len, 0;solvec LinearSolvenormcoeffs, rhs;tancoeffs ParallelTableATani, j, len, i, len, j, len1;cosVec TableCosthetai , i, len;VelVec cosVec Totaltancoeffs.solvec, 2;cntrlpts Tablexi, yi, i, len;pnllens TableSi, i, len;pnlangs Tablethetai, i, len;Clearx, y, XB, YB, S, theta;Threadcntrlpts, pnlangs, pnllens, VelVec, 1 VelVec^2

    pressureCoefficientsParallel PressureCoefficientsParallelpnlPts, 6 ; AbsoluteTiming

    Here are 12 rows of our result again.DisplayTableSortpressureCoefficientsParallel ;; 12, ColumnHeadings

    "Panel midpoints", "Panel angle", "Panel lengths", "Velocity", "Pressure"

    Checking our results visually:PDVectorPlotpnlPts,pressureCoefficientsParallelAll, 1, 6 , ImageSize Large

    Further Resources: gridMathematica QuickStart ServiceThe gridMathematica QuickStart Service is a two-day on-site event designed for organizations with an existingcluster environment. During this time an experienced Wolfram Research consultant helps get your team up andrunning by providing the following:

    Installation and Configuration: Help in installation and configuring gridMathematica software for your

    cluster, including a launch notebook for launching kernels on your cluster.

    Training: A half-day long class M225: Parallel Computing with Mathematica.

    Project Specific Support: A half-day of gridMathematica discussion specific to your project, including

    parallelization and debugging strategies.

    The service is designed for organizations that:

    have purchased or plan to purchase gridMathematica

    are starting to or plan to use gridMathematica for a specific project

    would like to become more effective in using gridMathematica.

    For more information on the gridMathematica QuickStart Service, please contact [email protected]

    Further Resources: MathematicaThis notebook requires Mathematica 7 or higher to run. You should be able to change the Getting Started slide tothe appropriate setting for LaunchKernels for your environment, and then run the notebook as normal. Note that

    54 GIST.nb

  • some cells have been set to be unevaluatable, for example the benchmark cells. If you wish to repeat the bench-marks, simply select the cells and from the Cell menu, choose Cell Properties Evaluatable. Documentation for parallel functionality: reference.wolfram.com/mathematica/ParallelTools/tutorial/Overview

    Information about Mathematica: www.wolfram.com/products/mathematica

    Information about gridMathematica: www.wolfram.com/products/gridmathematica

    To request a trial copy of Mathematica, please stay until the seminar has ended, and you will be given the opportu-nity to request one as a part of the seminar survey.

    If you are interested in evaluating gridMathematica, please contact [email protected].

    GIST.nb 55

  • Mathematica Tech TalkGPU Computing in Mathematica

    What and Why GPU Computing?

    What is GPU Computing? Graphical card is a piece of hardware devoted to communicating with the CPU and the display.

    Graphical card has a processing unit developed to perform graphics operation fast.

    Graphics cards evolved into devices capable doing general computation fast.

    GPU can be used to perform general computation.

    What are CUDA and OpenCL? Programming languages/environments that allow one to program the GPU to perform general computation.

    CUDA works only on NVIDIA hardware and is proprietary.

    OpenCL works on AMD or NVIDIA hardware and is an open standard.

    Why GPU Computing? Only reason to use the GPU is for speed.

    In terms of raw floating point operations power, the GPU beats the CPU.

    Why so Fast? Speed comes from the hardware design.

    Whereas CPUs have a small number of arithmetic processing units (APU) in favor of larger cache and logic

    control, the GPU has very limited logic control units and cache in favor of a large number of APUs.

    Because of the architecture, the CPUs are 2-,4-,6-, or 8-cores whereas the GPU has 256-, 512-, 1024-cores.

    56 GIST.nb

  • What are CUDALink and OpenCLLink?

    What are CUDALink and OpenCLLink? A way to use the Graphical Processing Unit (GPU) from within Mathematica integrating them with the users

    work flow

    A way to load GPU programs into Mathematica and use them as functions

    What are CUDALink and OpenCLLink Not? An attempt to make all Mathematica functions utilize the GPU

    Meant to automatically speed up Mathematica code

    System Requirements OpenCL capable hardware

    Recent video card driver

    For CUDA programming, a supported C compiler is required (VisualStudio or GCC). A C compiler is not needed

    for OpenCL programming.

    OpenCLLink supports both NVIDIA and AMD/ATI hardware.

    Computing with CUDALink and OpenCLLinkCUDALink contains many built-in functions for performing linear algebra, list processing, image processing, andFourier analysis. Those functions require no GPU knowledge, but do require the system to be CUDALinkcompatible.

    To check if the system is compatible with CUDALink, first load the CUDALink application.Needs"CUDALink`"

    GIST.nb 57

  • Once loaded, one can check for CUDALink compatibility by using CUDAQ.CUDAQ

    Information about the detected GPU hardware can be found by using CUDAInformation.CUDAInformation

    Computing with CUDALink and OpenCLLinkOnce system compatibility is verified, the built-in CUDALink functions can be used. Here, we multiply two matrices.

    CUDADot

    1 1 1 1 12 2 2 2 23 3 3 3 34 4 4 4 45 5 5 5 5

    ,

    0 1 1 1 01 1 1 1 11 1 1 1 11 1 1 1 10 1 1 1 0

    MatrixForm

    Perform an image convolution on an image.

    CUDAImageConvolve , GaussianMatrix3

    A finance operation.ListPlot3DParallelMapCUDAFinancialDerivative"American", "Put",

    "StrikePrice" 80., "Barriers" 100, "Expiration" ,"CurrentPrice" Range30., 130., 1, "InterestRate" 0.06,"Volatility" 0.45, "Dividend" 0.02, "Rebate" 5. &,

    Range0.2, 10, 0.2, DataRange 30, 130, 0.2, 10,AxesLabel "Stock", "Time", "Option"

    Programming with CUDALink CUDALink and OpenCLLink also allow users to load user programs into Mathematica. These functions can then

    be used as any Mathematica function and leverage Mathematicas programming language, visualization

    functionality, import/export capabilities, and its many other features.

    Functions are executed using the CUDA or OpenCL execution model.

    The CUDA and OpenCL execution model relies on launching a grid (which is divided into blocks). You load the

    function onto the grid and execute that function on different data elements.

    58 GIST.nb

  • Basic CUDALink ProgrammingGiven the following input,

    listSize 1024;data RangelistSize;

    suppose you want to compute the following:Forindex 1, index listSize, index,dataindex dataindex 2

    If you translate the above into C, you get the following:

    void addTwo(mint * in, mint * out, mint length) {mint index;for (index = 0; index < length; index++) {

    data[index] = data[index] + 2;}

    }In CUDA the above C code becomes:

    __global__ void addTwo(mint * in, mint * out, mint length) {int index = threadIdx.x + blockIdx.x*blockDim.x;data[index] = data[index] + 2;

    }We will use and discuss the meaning of each line next.

    Basic CUDALink ProgrammingThis CUDA program adds two to each element in a dataset.

    src "__global__ void addTwomint data, mint length

    int index threadIdx.x blockIdx.xblockDim.x;if index length

    dataindex dataindex 2;

    ";

    The program is loaded using:cudaAddTwo CUDAFunctionLoadsrc, "addTwo", _Integer, _Integer, 16

    This defines the input elements.listSize 64;data RangelistSize;

    This runs the function on the input, returning the result.cudaAddTwodata, listSize

    Understanding a Basic CUDA ProgramOn the CUDA source side, the function head is:

    GIST.nb 59

  • __global __ void addTwo mint data, mint length __global__ tells the compiler that this functions will run on the GPU and is callable from the CPU void a __global__ function cannot return a value, so the return type will always be void addTwo is the CUDA kernel name (mint * data, mint length) are the function arguments. Here mint is the Mathematica integer

    Getting the index:

    int index threadIdx.x blockIdx.x blockDim.x; int index defines the index variable as an int threadIdx.x is the offset of the CUDA thread in the x direction blockIdx.x is the offset of the CUDA block in the x direction blockDim.x is the size of the CUDA block in the x direction

    if index length makes sure that we do not write into memory what we did not allocate (possible buffer overflow otherwise)

    The body of the CUDA function is

    dataindex dataindex 2; stores data[index] + 2 into data[index] (i.e., increments data[index] by 2)

    Understanding a Basic CUDALink ProgramOn the Mathematica side, all you need to let CUDALink know is the program source, the function to load, theargument types, and the block dimensions. Consider the following:

    The argument types are similar in syntax to that of Compile. Input lists (or C pointers) are denoted as a list andthen the type, while scalars are denoted by just the type.

    So, in our example we had the following CUDA function prototype:

    __global __ void addTwo mint in, mint lengthTherefore, our CUDAFunctionLoad command is:

    CUDAFunctionLoadsrc, "addTwo", _Integer, _Integer, 256

    Programming with OpenCLLink Same principles as programming CUDALink.

    Kernel has a change in keywords.

    60 GIST.nb

  • CUDA OpenCL

    __global__ __kernel

    int f, __global int f,

    threadIdx get_local_id

    blockIdx get_group_id

    blockDim get_local_size

    threadIdxblockIdxblockDim get_global_id

    __shared__ __local

    From the Mathematica point of view, Mathematica provides the same function loading usage.

    Why Use CUDALink and OpenCLLink?

    Simplified Workflow

    Traditional GPU programming requires the user to manage compilation, memory, and threads.

    CUDALink and OpenCLLink simplify the workflow by allowing the user to concentrate only on the algorithmic part of the program (not the setup part).

    This results in a more streamlined workflow.

    GIST.nb 61

  • GPU API Independence Support for CUDA and OpenCL out of the box, as well as offering a way to execute commands in parallel on the

    CPU makes Mathematica a very attractive platform for HPC development and deployment.

    The syntax of CUDAFunctionLoad and OpenCLFunctionLoad are the same. The user only needs to change the

    kernel and calls from CUDA* to OpenCL*.

    Coupled with Mathematicas symbolic code generation tools, this means that users can write code that executes

    code that runs on either CUDA or OpenCL depending on which is available.

    Platform Independence Mathematica is platform independent and as a result CUDALink and OpenCLLink are as well. The compiler will

    automatically detect and use the proper compiler and flags on the system.

    Using CUDALink and OpenCLLinks abstract types, like Real_t allows the user to use the maximum precision on the system.

    We also give users the ability to find out whether a kernel is launched on NVIDIA or AMD/ATI hardware. This

    allows for lines like the following in the kernel.

    #ifdef USING_DOUBLE_PRECISIONQ#ifdef OPENCLLINK_USING_NVIDIA

    #pragma OPENCL EXTENSION cl_khr_fp64 : enable#else

    #pragma OPENCL EXTENSION cl_amd_fp64 : enable#endif

    #endif

    Access to Mathematicas Rich Set of Programming ConstructsUsers can use dynamics to create animations of data. In this example we write a kernel that performs the game oflife evolution rules.

    62 GIST.nb

  • src "__kernel void gol_kernel__global

    mint prev, __global mint nxt, mint width, mint height int xIndex get_global_id0, yIndex get_global_id1;int index xIndex yIndexwidth;int ii, jj, curr, neighbrs;if xIndex width && yIndex height curr previndex;neighbrs 0;for ii 1, neighbrs curr; ii 1; ii if xIndex ii 0 && xIndexii width for jj 1; jj 1; jj if yIndexjj 0 && yIndexjj heightneighbrs prevxIndex ii yIndexjjwidth;

    if curr 1nxtindex neighbrs 2 neighbrs 3 ? 1 : 0;

    elsenxtindex neighbrs 3 ? 1 : 0;

    ";

    The function is then loaded into Mathematica.gol OpenCLFunctionLoadsrc, "gol_kernel",

    _Integer, "Input", _Integer, "Output", _Integer, _Integer,16, 16, "Defines" "mint" "int";

    This sets the initial state and output state.initialState RandomChoice0.7, 0.3 0, 1, 512, 512;outputState ConstantArray0, 512, 512;

    A dynamic visualization can then be performed.DynamicRefreshinitialState FirstgolinitialState, outputState, 512, 512;ArrayPlotinitialState, ImageSize Medium,UpdateInterval 1 60

    Scalable to Multiple GPUs or Multiple Machines

    CUDALink and OpenCLLink are scalable across multiple GPUs using Mathematicas parallel tools. They are alsoscalable across machines using Wolfram Lightweight Grid Mathematica.

    Integration into Mathematica Integrated into the product with many built-in functions.

    GIST.nb 63

  • Support for both single and double precision cards.

    Both CUDA and OpenCL capabilities are built in.

    64 GIST.nb

  • Mathematica Tech TalkIntroducing MathModelica

    The MathModelica User Interface

    Model Analyze Simulate

    GIST.nb 65

  • Simulating an Already Existing Model

    Simulating an Already Existing Model

    Using Your Model in Another Model

    66 GIST.nb

  • Using Your Model in Another Model

    Analyze and design in Mathematica

    MathModelica: Model, Simulate, Analyze

    GIST.nb 67