You are previewing Scientific Computing with Scala.
O'Reilly logo
Scientific Computing with Scala

Book Description

Learn to solve scientific computing problems using Scala and its numerical computing, data processing, concurrency, and plotting libraries

About This Book

  • Parallelize your numerical computing code using convenient and safe techniques.

  • Accomplish common high-performance, scientific computing goals in Scala.

  • Learn about data visualization and how to create high-quality scientific plots in Scala

  • Who This Book Is For

    Scientists and engineers who would like to use Scala for their scientific and numerical computing needs. A basic familiarity with undergraduate level mathematics and statistics is expected but not strictly required. A basic knowledge of Scala is required as well as the ability to write simple Scala programs. However, complicated programming concepts are not used in the book. Anyone who wants to explore using Scala for writing scientific or engineering software will benefit from the book.

    What You Will Learn

  • Write and read a variety of popular file formats used to store scientific data

  • Use Breeze for linear algebra, optimization, and digital signal processing

  • Gain insight into Saddle for data analysis

  • Use ScalaLab for interactive computing

  • Quickly and conveniently write safe parallel applications using Scala's parallel collections

  • Implement and deploy concurrent programs using the Akka framework

  • Use the Wisp plotting library to produce scientific plots

  • Visualize multivariate data using various visualization techniques

  • In Detail

    Scala is a statically typed, Java Virtual Machine (JVM)-based language with strong support for functional programming. There exist libraries for Scala that cover a range of common scientific computing tasks – from linear algebra and numerical algorithms to convenient and safe parallelization to powerful plotting facilities. Learning to use these to perform common scientific tasks will allow you to write programs that are both fast and easy to write and maintain.

    We will start by discussing the advantages of using Scala over other scientific computing platforms. You will discover Scala packages that provide the functionality you have come to expect when writing scientific software. We will explore using Scala's Breeze library for linear algebra, optimization, and signal processing. We will then proceed to the Saddle library for data analysis. If you have experience in R or with Python's popular pandas library you will learn how to translate those skills to Saddle. If you are new to data analysis, you will learn basic concepts of Saddle as well. Well will explore the numerical computing environment called ScalaLab. It comes bundled with a lot of scientific software readily available. We will use it for interactive computing, data analysis, and visualization. In the following chapters, we will explore using Scala's powerful parallel collections for safe and convenient parallel programming. Topics such as the Akka concurrency framework will be covered. Finally, you will learn about multivariate data visualization and how to produce professional-looking plots in Scala easily. After reading the book, you should have more than enough information on how to start using Scala as your scientific computing platform

    Style and approach

    Examples are provided on how to use Scala to do basic numerical and scientific computing tasks. All the concepts are illustrated with more involved examples in each chapter. The goal of the book is to allow you to translate existing experience in scientific computing to Scala.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Scientific Computing with Scala
      1. Table of Contents
      2. Scientific Computing with Scala
      3. Credits
      4. About the Author
      5. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Introducing Scientific Computing with Scala
        1. Why Scala for scientific computing?
          1. What are the advantages compared to C/C++/Java?
          2. What are the advantages compared to MATLAB/Python/R?
          3. Scala does parallelism well
          4. Any downsides?
        2. Numerical computing packages for Scala
          1. Scalala
          2. Breeze
          3. ScalaLab
        3. Data analysis packages for Scala
          1. Saddle
          2. MLlib
        4. Other scientific software
          1. FACTORIE
          2. Cassovary
          3. Figaro
        5. Alternatives for doing plotting
        6. Using Emacs as the Scala IDE
        7. Profiling Scala code
        8. Debugging Scala code
        9. Building, testing, and distributing your Scala software
          1. Directory structure
          2. Testing Scala code with the help of SBT
          3. ENSIME and SBT integration
          4. Distributing your software
        10. Mixing Java and Scala code
        11. Summary
      9. 2. Storing and Retrieving Data
        1. Reading and writing CSV files
          1. Reading files in Scala
          2. Parsing CSV data
          3. Processing CSV data
        2. Reading and writing JSON files
          1. Spray-JSON
          2. SON of JSON
          3. Argonaut
        3. Reading and writing XML files
        4. Database access using JDBC
        5. Database access using Slick
          1. Plain SQL
        6. Reading and writing HDF5 files
        7. Summary
      10. 3. Numerical Computing with Breeze
        1. Using Breeze in your project
        2. Basic Breeze data structures
          1. DenseVector
          2. DenseMatrix
          3. Indexing and slicing
          4. Reshaping
          5. Concatenation
        3. Statistical computing with Breeze
        4. Optimization
        5. Signal processing
          1. Fourier transforms
          2. Other signal processing functionality
        6. Cheat sheet
          1. Creating matrices and vectors
          2. Operations on matrices and vectors
        7. Summary
      11. 4. Using Saddle for Data Analysis
        1. Installing Saddle
        2. Basic Saddle data structures
          1. Using the Vec structure
            1. Using arithmetic operations in Vec
            2. Data access in Vec
            3. Implementing the slice method in Vec
            4. Statistic calculation in Vec
          2. Using the Mat structure
            1. Creating a matrix with Mat
            2. Applying arithmetic operators in Mat structures
            3. Using Matrix in the Mat structure
          3. Series
            1. Implementing the groupBy method in the Series structure
            2. Applying the transform method in Series
            3. Using numerical operators in Series
            4. Joining Series using the join operation
              1. Applying index.LeftJoin
              2. Applying index.RightJoin
              3. Applying index.InnerJoin
              4. Applying index.OuterJoin
          4. Frame
            1. Using the rowAt method in Frame
            2. Using the sortedColsBy method in Frame
        3. Data analysis with Saddle
          1. Using Breeze with Saddle
        4. Summary
      12. 5. Interactive Computing with ScalaLab
        1. Installing and running ScalaLab
        2. Basic ScalaSci data structures
          1. Vector
          2. Matrix
          3. Other ScalaSci functionality
        3. Data storage and retrieval
        4. Plotting with ScalaLab
        5. Other ScalaLab features
          1. Doing symbolic algebra using symja
        6. Summary
      13. 6. Parallel Programming in Scala
        1. Programming with Scala threads
          1. A simple Scala thread example
          2. Synchronization
          3. Monte-Carlo pi calculation
        2. Using Scala's parallel collections
        3. Agent-based concurrency with the Akka framework
          1. Monte-Carlo pi revisited
          2. Using routing
          3. Waiting for a reply
        4. Summary
      14. 7. Cluster Computing Using Scala
        1. Using MPJ Express for distributed computing
          1. Setting up and running MPJ Express
          2. Using Send and Recv
          3. Sending Scala objects in MPJ Express messages
          4. Non-blocking communication
          5. Scatter and Gather
          6. Setting up MPJ Express on clusters
        2. Using an Akka cluster for distributed computing
        3. Summary
      15. 8. Scientific Plotting with Scala
        1. Plotting with JFreeChart
          1. Using JFreeChart in your project
          2. Creating a line plot
          3. Creating a histogram
          4. Creating a bar chart
          5. Creating a box-and-whisker chart
          6. Other plot types
          7. Saving charts to a file
        2. Plotting with scala-chart
          1. Installing scala-chart
          2. Creating a line plot
          3. Creating a histogram
          4. Creating a bar chart
          5. Creating a box-and-whisker chart
          6. Saving charts to a file
        3. Plotting with Wisp
          1. Creating a line plot
          2. Creating a histogram
          3. Creating a bar chart
          4. Creating a box-and-whisker chart
          5. Creating a linear regression plot
          6. Interacting with the server
        4. Summary
      16. 9. Visualizing Multi-Dimensional Data in Scala
        1. Obtaining data to visualize
        2. Andrews curve
        3. Parallel coordinates
        4. Scatter plot matrix
        5. Sammon mapping
        6. Improving the program
        7. Summary
      17. Index