<!DOCTYPE html>
<html>
<head>
  <meta name="databricks-html-version" content="1">
<title>LAlgCheatSheet - Databricks</title>

<meta charset="utf-8">
<meta name="google" content="notranslate">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<link rel="stylesheet"
  href="https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700">

<link rel="stylesheet" type="text/css" href="https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/lib/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/lib/jquery-ui-bundle/jquery-ui.min.css">
<link rel="stylesheet" type="text/css" href="https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/css/main.css">
<link rel="stylesheet" href="https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/css/print.css" media="print">
<link rel="icon" type="image/png" href="https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/img/favicon.ico"/>
<script>window.settings = {"sparkDocsSearchGoogleCx":"004588677886978090460:_rj0wilqwdm","dbcForumURL":"http://forums.databricks.com/","dbfsS3Host":"https://databricks-prod-storage-sydney.s3.amazonaws.com","enableThirdPartyApplicationsUI":false,"enableClusterAcls":false,"notebookRevisionVisibilityHorizon":0,"enableTableHandler":true,"isAdmin":true,"enableLargeResultDownload":false,"nameAndEmail":"Raazesh Sainudiin (r.sainudiin@math.canterbury.ac.nz)","enablePresentationTimerConfig":true,"enableFullTextSearch":true,"enableElasticSparkUI":true,"clusters":true,"hideOffHeapCache":false,"applications":false,"useStaticGuide":false,"fileStoreBase":"FileStore","configurableSparkOptionsSpec":[{"keyPattern":"spark\\.kryo(\\.[^\\.]+)+","valuePattern":".*","keyPatternDisplay":"spark.kryo.*","valuePatternDisplay":"*","description":"Configuration options for Kryo serialization"},{"keyPattern":"spark\\.io\\.compression\\.codec","valuePattern":"(lzf|snappy|org\\.apache\\.spark\\.io\\.LZFCompressionCodec|org\\.apache\\.spark\\.io\\.SnappyCompressionCodec)","keyPatternDisplay":"spark.io.compression.codec","valuePatternDisplay":"snappy|lzf","description":"The codec used to compress internal data such as RDD partitions, broadcast variables and shuffle outputs."},{"keyPattern":"spark\\.serializer","valuePattern":"(org\\.apache\\.spark\\.serializer\\.JavaSerializer|org\\.apache\\.spark\\.serializer\\.KryoSerializer)","keyPatternDisplay":"spark.serializer","valuePatternDisplay":"org.apache.spark.serializer.JavaSerializer|org.apache.spark.serializer.KryoSerializer","description":"Class to use for serializing objects that will be sent over the network or need to be cached in serialized form."},{"keyPattern":"spark\\.rdd\\.compress","valuePattern":"(true|false)","keyPatternDisplay":"spark.rdd.compress","valuePatternDisplay":"true|false","description":"Whether to compress serialized RDD partitions (e.g. for StorageLevel.MEMORY_ONLY_SER). Can save substantial space at the cost of some extra CPU time."},{"keyPattern":"spark\\.speculation","valuePattern":"(true|false)","keyPatternDisplay":"spark.speculation","valuePatternDisplay":"true|false","description":"Whether to use speculation (recommended off for streaming)"},{"keyPattern":"spark\\.es(\\.[^\\.]+)+","valuePattern":".*","keyPatternDisplay":"spark.es.*","valuePatternDisplay":"*","description":"Configuration options for ElasticSearch"},{"keyPattern":"es(\\.([^\\.]+))+","valuePattern":".*","keyPatternDisplay":"es.*","valuePatternDisplay":"*","description":"Configuration options for ElasticSearch"},{"keyPattern":"spark\\.(storage|shuffle)\\.memoryFraction","valuePattern":"0?\\.0*([1-9])([0-9])*","keyPatternDisplay":"spark.(storage|shuffle).memoryFraction","valuePatternDisplay":"(0.0,1.0)","description":"Fraction of Java heap to use for Spark's shuffle or storage"},{"keyPattern":"spark\\.streaming\\.backpressure\\.enabled","valuePattern":"(true|false)","keyPatternDisplay":"spark.streaming.backpressure.enabled","valuePatternDisplay":"true|false","description":"Enables or disables Spark Streaming's internal backpressure mechanism (since 1.5). This enables the Spark Streaming to control the receiving rate based on the current batch scheduling delays and processing times so that the system receives only as fast as the system can process. Internally, this dynamically sets the maximum receiving rate of receivers. This rate is upper bounded by the values `spark.streaming.receiver.maxRate` and `spark.streaming.kafka.maxRatePerPartition` if they are set."},{"keyPattern":"spark\\.streaming\\.receiver\\.maxRate","valuePattern":"^([0-9]{1,})$","keyPatternDisplay":"spark.streaming.receiver.maxRate","valuePatternDisplay":"numeric","description":"Maximum rate (number of records per second) at which each receiver will receive data. Effectively, each stream will consume at most this number of records per second. Setting this configuration to 0 or a negative number will put no limit on the rate. See the deployment guide in the Spark Streaming programing guide for mode details."},{"keyPattern":"spark\\.streaming\\.kafka\\.maxRatePerPartition","valuePattern":"^([0-9]{1,})$","keyPatternDisplay":"spark.streaming.kafka.maxRatePerPartition","valuePatternDisplay":"numeric","description":"Maximum rate (number of records per second) at which data will be read from each Kafka partition when using the Kafka direct stream API introduced in Spark 1.3. See the Kafka Integration guide for more details."},{"keyPattern":"spark\\.streaming\\.kafka\\.maxRetries","valuePattern":"^([0-9]{1,})$","keyPatternDisplay":"spark.streaming.kafka.maxRetries","valuePatternDisplay":"numeric","description":"Maximum number of consecutive retries the driver will make in order to find the latest offsets on the leader of each partition (a default value of 1 means that the driver will make a maximum of 2 attempts). Only applies to the Kafka direct stream API introduced in Spark 1.3."},{"keyPattern":"spark\\.streaming\\.ui\\.retainedBatches","valuePattern":"^([0-9]{1,})$","keyPatternDisplay":"spark.streaming.ui.retainedBatches","valuePatternDisplay":"numeric","description":"How many batches the Spark Streaming UI and status APIs remember before garbage collecting."}],"enableReactNotebookComments":true,"enableResetPassword":true,"enableJobsSparkUpgrade":true,"sparkVersions":[{"key":"1.3.x-ubuntu15.10","displayName":"Spark 1.3.0","packageLabel":"spark-1.3-jenkins-ip-10-30-9-162-U0c2673ac85-Sa2ee4664b2-2016-02-09-02:05:59.455061","upgradable":true,"deprecated":false,"customerVisible":true},{"key":"1.4.x-ubuntu15.10","displayName":"Spark 1.4.1","packageLabel":"spark-1.4-jenkins-ip-10-30-9-162-U0c2673ac85-S33a1e4b9c6-2016-02-09-02:05:59.455061","upgradable":true,"deprecated":false,"customerVisible":true},{"key":"1.5.x-ubuntu15.10","displayName":"Spark 1.5.2","packageLabel":"spark-1.5-jenkins-ip-10-30-9-162-U0c2673ac85-S5917a1044d-2016-02-09-02:05:59.455061","upgradable":true,"deprecated":false,"customerVisible":true},{"key":"1.6.x-ubuntu15.10","displayName":"Spark 1.6.0","packageLabel":"spark-1.6-jenkins-ip-10-30-9-162-U0c2673ac85-Scabba801f3-2016-02-09-02:05:59.455061","upgradable":true,"deprecated":false,"customerVisible":true},{"key":"master","displayName":"Spark master (dev)","packageLabel":"","upgradable":true,"deprecated":false,"customerVisible":false}],"enableRestrictedClusterCreation":false,"enableFeedback":false,"defaultNumWorkers":8,"serverContinuationTimeoutMillis":10000,"driverStderrFilePrefix":"stderr","driverStdoutFilePrefix":"stdout","enableSparkDocsSearch":true,"prefetchSidebarNodes":true,"sparkHistoryServerEnabled":true,"sanitizeMarkdownHtml":true,"enableIPythonImportExport":true,"enableNotebookHistoryDiffing":true,"branch":"2.12.3","accountsLimit":-1,"enableNotebookGitBranching":true,"local":false,"displayDefaultContainerMemoryGB":6,"deploymentMode":"production","useSpotForWorkers":false,"enableUserInviteWorkflow":false,"enableStaticNotebooks":true,"dbcGuideURL":"#workspace/databricks_guide/00 Welcome to Databricks","enableCssTransitions":true,"pricingURL":"https://databricks.com/product/pricing","enableClusterAclsConfig":false,"orgId":0,"enableNotebookGitVersioning":true,"files":"files/","enableDriverLogsUI":true,"disableLegacyDashboards":false,"enableWorkspaceAclsConfig":true,"dropzoneMaxFileSize":4096,"enableNewDashboardViews":false,"driverLog4jFilePrefix":"log4j","enableMavenLibraries":true,"displayRowLimit":1000,"defaultSparkVersion":{"key":"1.5.x-ubuntu15.10","displayName":"Spark 1.5.2","packageLabel":"spark-1.5-jenkins-ip-10-30-9-162-U0c2673ac85-S5917a1044d-2016-02-09-02:05:59.455061","upgradable":true,"deprecated":false,"customerVisible":true},"clusterPublisherRootId":5,"enableLatestJobRunResultPermalink":true,"disallowAddingAdmins":false,"enableSparkConfUI":true,"enableOrgSwitcherUI":false,"clustersLimit":-1,"enableJdbcImport":true,"logfiles":"logfiles/","enableWebappSharding":false,"enableClusterDeltaUpdates":true,"csrfToken":"3dde4939-9fec-41a8-bc90-741d1aa44ca4","useFixedStaticNotebookVersionForDevelopment":false,"enableBasicReactDialogBoxes":true,"requireEmailUserName":true,"enableDashboardViews":false,"dbcFeedbackURL":"http://feedback.databricks.com/forums/263785-product-feedback","enableWorkspaceAclService":true,"someName":"Raazesh Sainudiin","enableWorkspaceAcls":true,"gitHash":"0c2673ac858e227cad536fdb45d140aeded238db","userFullname":"Raazesh Sainudiin","enableClusterCreatePage":false,"enableImportFromUrl":true,"enableMiniClusters":false,"enableWebSocketDeltaUpdates":true,"enableDebugUI":false,"showHiddenSparkVersions":false,"allowNonAdminUsers":true,"userId":100005,"dbcSupportURL":"","staticNotebookResourceUrl":"https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/","enableSparkPackages":true,"enableHybridClusterType":false,"enableNotebookHistoryUI":true,"availableWorkspaces":[{"name":"Workspace 0","orgId":0}],"enableFolderHtmlExport":true,"enableSparkVersionsUI":true,"databricksGuideStaticUrl":"","enableHybridClusters":true,"notebookLoadingBackground":"#fff","enableNewJobRunDetailsPage":true,"enableDashboardExport":true,"user":"r.sainudiin@math.canterbury.ac.nz","enableServerAutoComplete":true,"enableStaticHtmlImport":true,"defaultMemoryPerContainerMB":6000,"enablePresenceUI":true,"tablesPublisherRootId":7,"enableNewInputWidgetUI":false,"accounts":true,"enableNewProgressReportUI":true,"defaultCoresPerContainer":4};</script>
<script>var __DATABRICKS_NOTEBOOK_MODEL = {"version":"NotebookV1","origId":44080,"name":"LAlgCheatSheet","language":"scala","commands":[{"version":"CommandV1","origId":132572,"guid":"8a2a7810-f948-49b3-b1b1-d33dddf47e0b","subtype":"command","commandType":"auto","position":0.25,"command":"%md\n\n# [Scalable Data Science](http://www.math.canterbury.ac.nz/~r.sainudiin/courses/ScalableDataScience/)\n\n\n### prepared by [Raazesh Sainudiin](https://nz.linkedin.com/in/raazesh-sainudiin-45955845) and [Sivanand Sivaram](https://www.linkedin.com/in/sivanand)\n\n*supported by* [![](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/images/databricks_logoTM_200px.png)](https://databricks.com/)\nand \n[![](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/images/AWS_logoTM_200px.png)](https://www.awseducate.com/microsite/CommunitiesEngageHome)","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"fcf6e93e-31ea-4d3e-9fe1-6183667f3fd6"},{"version":"CommandV1","origId":132660,"guid":"fd4dcf66-328e-45b2-9d95-df8faf67c44f","subtype":"command","commandType":"auto","position":0.3125,"command":"%md\nThe [html source url](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/db/xtraResources/LinearAlgebra/LAlgCheatSheet.html) of this databricks HOMEWORK notebook and its recorded Uji ![Image of Uji, Dogen's Time-Being](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/images/UjiTimeBeingDogen.png \"uji\"):\n\n[![sds/uji/week5/09_LinearAlgebraIntro/017_LAlgIntro-Homework-db/xtraResources/LinearAlgebra/LAlgCheatSheet](http://img.youtube.com/vi/y6F-e6m1m2s/0.jpg)](https://www.youtube.com/v/y6F-e6m1m2s?rel=0&autoplay=1&modestbranding=1&start=1547&end=1673)\n","commandVersion":0,"state":"error","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"183bea98-563c-429e-adf3-dfe3673ba071"},{"version":"CommandV1","origId":132573,"guid":"a3d4f72b-4a69-4916-8fd1-642eaa3986d3","subtype":"command","commandType":"auto","position":0.375,"command":"%md\nThe [html source url](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/db/week5/09_LinearAlgebraIntro/017_LAlgIntro.html) of the context/parent databricks notebook (for this databricks HOMEWORK notebook) and its recorded Uji ![Image of Uji, Dogen's Time-Being](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/images/UjiTimeBeingDogen.png \"uji\"):\n\n[![sds/uji/week5/09_LinearAlgebraIntro/017_LAlgIntro](http://img.youtube.com/vi/y6F-e6m1m2s/0.jpg)](https://www.youtube.com/v/y6F-e6m1m2s?rel=0&autoplay=1&modestbranding=1&start=0&end=2634)\n","commandVersion":0,"state":"error","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"e53fdebd-dffd-4cd8-a397-afd40c472f04"},{"version":"CommandV1","origId":132574,"guid":"7fe9a517-77a1-430c-9330-440fa92136f3","subtype":"command","commandType":"auto","position":0.5,"command":"%md\nThis is from \n* [https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheet](https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheet)","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"506ca0aa-cf7f-4503-838d-011b3283ddc1"},{"version":"CommandV1","origId":132575,"guid":"46be836d-8b7f-4c1e-abc9-3deee468f49d","subtype":"command","commandType":"auto","position":0.75,"command":"%md\n## Core Concepts\n\nCompared to other numerical computing environments, Breeze matrices\ndefault to column major ordering, like Matlab, but indexing is 0-based,\nlike Numpy. Breeze has as its core concepts matrices and column vectors.\nRow vectors are normally stored as matrices with a single row. This\nallows for greater type safety with the downside that conversion of row\nvectors to column vectors is performed using a transpose-slice\n(`a.t(::,0)`) instead of a simple transpose (`a.t`).\n\n[[UFunc|Universal Functions]]s are very important in Breeze. Once you get a feel for the syntax (i.e. what's in this section), it might be worthwhile to read the first half of the UFunc wiki page. (You can skip the last half that involves implementing your own UFuncs...until you're ready to contribute to Breeze!)","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"69004587-bf67-4461-86c6-655e23caf60f"},{"version":"CommandV1","origId":132576,"guid":"e9218eaa-dc25-404a-b05d-99507cf10f58","subtype":"command","commandType":"auto","position":0.875,"command":"%md\n## Quick Reference\n\nThe following table assumes that Numpy is used with `from numpy import *` and Breeze with:","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"1a8e2c8c-c0cd-4729-8334-67c60958945f"},{"version":"CommandV1","origId":132577,"guid":"6f486384-e712-4e0e-b6d4-d2706ef936b7","subtype":"command","commandType":"auto","position":0.9375,"command":"import breeze.linalg._\nimport breeze.numerics._","commandVersion":0,"state":"finished","results":{"type":"html","data":"<div class=\"ansiout\">import breeze.linalg._\nimport breeze.numerics._\n</div>","arguments":{},"addedWidgets":{},"removedWidgets":[]},"errorSummary":null,"error":null,"startTime":1.457213380466E12,"submitTime":1.457213325857E12,"finishTime":1.457213380566E12,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"siva.anand@gmail.com","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"3db62e17-2ce0-48ff-9698-1ee092fcef27"},{"version":"CommandV1","origId":132578,"guid":"d04c1ee2-476f-4020-8fa5-31ec9621630d","subtype":"command","commandType":"auto","position":0.96875,"command":"%md\n### Creation\n\n| Operation                       | Breeze                                        | Matlab            | Numpy           |R\n| ------------------------------- | --------------------------------------------- | ----------------- | ----------------|-------------------\n| Zeroed matrix                   | `DenseMatrix.zeros[Double](n,m)`              | `zeros(n,m)`      | `zeros((n,m))`  |`mat.or.vec(n, m)`\n| Zeroed vector                   | `DenseVector.zeros[Double](n)`                | `zeros(n,1)`      | `zeros(n)`      |`mat.or.vec(n, 1)`\n| Vector of ones                  | `DenseVector.ones[Double](n)`                 | `ones(n,1)`       | `ones(n)`       |`mat.or.vec(n, 1) + 1`\n| Vector of particular number     | `DenseVector.fill(n){5.0}`                    | `ones(n,1) * 5`   | `ones(n) * 5`   |`(mat.or.vec(5, 1) + 1) * 5`\n| range given stepsize            | `DenseVector.range(start,stop,step)` or `Vector.rangeD(start,stop,step)`          |                 ||`seq(start,stop,step)`\n| n element range                 | `linspace(start,stop,numvals)`    | `linspace(0,20,15)`                |                             ||\n| Identity matrix                 | `DenseMatrix.eye[Double](n)`                  | `eye(n)`          | `eye(n)`        |`identity(n)`\n| Diagonal matrix                 | `diag(DenseVector(1.0,2.0,3.0))`              | `diag([1 2 3])`   | `diag((1,2,3))` |`diag(c(1,2,3))`\n| Matrix inline creation          | `DenseMatrix((1.0,2.0), (3.0,4.0))`           | `[1 2; 3 4]`      | `array([ [1,2], [3,4] ])` |`matrix(c(1,2,3,4), nrow = 2, ncol = 2)`\n| Column vector inline creation   | `DenseVector(1,2,3,4)`                        | `[1 2 3 4]`       | `array([1,2,3,4])`|`c(1,2,3,4)`\n| Row vector inline creation      | `DenseVector(1,2,3,4).t`                      | `[1 2 3 4]'`      | `array([1,2,3]).reshape(-1,1)` |`t(c(1,2,3,4))`\n| Vector from function            | `DenseVector.tabulate(3){i => 2*i}`           |                   |                                ||\n| Matrix from function            | `DenseMatrix.tabulate(3, 2){case (i, j) => i+j}` |                |                                ||\n| Vector creation from array      | `new DenseVector(Array(1, 2, 3, 4))`          |                   |                                ||\n| Matrix creation from array      | `new DenseMatrix(2, 3, Array(11, 12, 13, 21, 22, 23))` |          |                                ||\n| Vector of random elements from 0 to 1 | `DenseVector.rand(4)`                   |                   |                                |`runif(4)` (requires stats library)\n| Matrix of random elements from 0 to 1 | `DenseMatrix.rand(2, 3)`                |                   |                                |`matrix(runif(6),2)` (requires stats library)","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"2aff1c5c-5a07-476a-9475-205af33381c0"},{"version":"CommandV1","origId":132579,"guid":"d4221fc2-7463-4cee-842e-563fb9a8297f","subtype":"command","commandType":"auto","position":0.984375,"command":"DenseMatrix.zeros[Double](2,3)","commandVersion":0,"state":"finished","results":{"type":"html","data":"<div class=\"ansiout\">res0: breeze.linalg.DenseMatrix[Double] = \n0.0  0.0  0.0  \n0.0  0.0  0.0  \n</div>","arguments":{},"addedWidgets":{},"removedWidgets":[]},"errorSummary":null,"error":null,"startTime":1.456990229738E12,"submitTime":1.456990186268E12,"finishTime":1.456990231234E12,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"r.sainudiin@math.canterbury.ac.nz","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"98bf056c-880c-4b55-844f-ae1d8c15fdb0"},{"version":"CommandV1","origId":132580,"guid":"e893cfb5-11a7-4f90-aa8f-067dbf41185b","subtype":"command","commandType":"auto","position":0.9921875,"command":"%py\nimport numpy as np","commandVersion":0,"state":"finished","results":{"type":"html","data":"<div class=\"ansiout\"></div>","arguments":{},"addedWidgets":{},"removedWidgets":[]},"errorSummary":null,"error":null,"startTime":1.456990261877E12,"submitTime":1.456990216573E12,"finishTime":1.456990261912E12,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"r.sainudiin@math.canterbury.ac.nz","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"0698e1ea-94d6-4bcd-b0f8-2ceb1ec05a94"},{"version":"CommandV1","origId":132581,"guid":"45c8ed95-2304-4f9d-b843-cf7f16dd9be9","subtype":"command","commandType":"auto","position":0.99609375,"command":"%py\nnp.zeros((2,3))","commandVersion":0,"state":"finished","results":{"type":"html","data":"<div class=\"ansiout\"><span class=\"ansired\">Out[</span><span class=\"ansired\">3</span><span class=\"ansired\">]: </span>\narray([[ 0.,  0.,  0.],\n       [ 0.,  0.,  0.]])\n</div>","arguments":{},"addedWidgets":{},"removedWidgets":[]},"errorSummary":"<span class=\"ansired\">TypeError</span>: data type not understood","error":"<div class=\"ansiout\"><span class=\"ansired\">---------------------------------------------------------------------------</span>\n<span class=\"ansired\">TypeError</span>                                 Traceback (most recent call last)\n<span class=\"ansigreen\">&lt;ipython-input-2-8f9f38c18b63&gt;</span> in <span class=\"ansicyan\">&lt;module&gt;</span><span class=\"ansiblue\">()</span>\n<span class=\"ansigreen\">----&gt; 1</span><span class=\"ansiyellow\"> </span>np<span class=\"ansiyellow\">.</span>zeros<span class=\"ansiyellow\">(</span><span class=\"ansicyan\">2</span><span class=\"ansiyellow\">,</span><span class=\"ansicyan\">3</span><span class=\"ansiyellow\">)</span><span class=\"ansiyellow\"></span>\n\n<span class=\"ansired\">TypeError</span>: data type not understood\n</div>","startTime":1.456990309608E12,"submitTime":1.456990266154E12,"finishTime":1.456990309633E12,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"r.sainudiin@math.canterbury.ac.nz","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"c1ebbf58-d077-4f6a-9587-b09fed441dcb"},{"version":"CommandV1","origId":132582,"guid":"d0626245-e3e9-48d7-b543-04d8a55ee5bc","subtype":"command","commandType":"auto","position":0.998046875,"command":"%r\nmat.or.vec(2,3)","commandVersion":0,"state":"finished","results":{"type":"html","data":"<pre style=\"font-size:10p\"></pre><pre style = 'font-size:10pt'>     [,1] [,2] [,3]\n[1,]    0    0    0\n[2,]    0    0    0</pre>","arguments":{},"addedWidgets":{},"removedWidgets":[]},"errorSummary":null,"error":null,"startTime":1.456990371677E12,"submitTime":1.456990322004E12,"finishTime":1.456990371685E12,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"r.sainudiin@math.canterbury.ac.nz","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"fbc17a3b-dd91-4002-9248-dfb65ae9e228"},{"version":"CommandV1","origId":132583,"guid":"2f97c432-0473-4173-83fa-451de7b78746","subtype":"command","commandType":"auto","position":0.9990234375,"command":"%md\n#### Reading and writing Matrices\n\nCurrently, Breeze supports IO for Matrices in two ways: Java serialization and csv. The latter comes from two functions: `breeze.linalg.csvread` and `breeze.linalg.csvwrite`. `csvread` takes a File, and optionally parameters for how the CSV file is delimited (e.g. if it is actually a tsv file, you can set tabs as the field delimiter.) and returns a [DenseMatrix](Data-Structures#densematrix). Similarly, `csvwrite` takes a File and a DenseMatrix, and writes the contents of a matrix to a file.\n\n### Indexing and Slicing\n\n|Operation                 |Breeze                                           |Matlab       |Numpy        |R\n|--------------------------|-------------------------------------------------|-------------|-------------|-----------\n|Basic Indexing            |`a(0,1)`                                         |`a(1,2)`     |`a[0,1]`     |`a[1,2]`\n|Extract subset of vector  |`a(1 to 4)` or `a(1 until 5)` or `a.slice(1,5)`  |`a(2:5)`     |`a[1:5]`     |`a[2:5]`\n|(negative steps)          |`a(5 to 0 by -1)`                                |`a(6:-1:1)`  |`a[5:0:-1]`  |\n|(tail)                    |`a(1 to -1)`                                     |`a(2:end)`   |`a[1:]`      |`a[2:length(a)]` or ` tail(a,n=length(a)-1)`\n|(last element)            |`a( -1 )`                                        |`a(end)`     |`a[-1]`      |`tail(a, n=1)`\n|Extract column of matrix  |`a(::, 2)`                                       |`a(:,3)`     |`a[:,2]`     |`a[,2]`","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"c21de16f-6ed6-413c-8c72-b0df0e642caa"},{"version":"CommandV1","origId":132584,"guid":"1220b32d-3632-4a20-8581-e86ec07fe7e1","subtype":"command","commandType":"auto","position":0.99951171875,"command":"val matrix = DenseMatrix.rand(2, 3)","commandVersion":0,"state":"finished","results":{"type":"html","data":"<div class=\"ansiout\">matrix: breeze.linalg.DenseMatrix[Double] = \n0.7899154259843089   0.23934068908674289  0.9745826078437245  \n0.07183200035510628  0.2350398776188658   0.9557467684951515  \n</div>","arguments":{},"addedWidgets":{},"removedWidgets":[]},"errorSummary":"<div class=\"ansiout\">&lt;console&gt;:32: error: not found: value DenseMatrix\n       val matrix = DenseMatrix.rand(2, 3)\n                    ^\n</div>","error":null,"startTime":1.457213386738E12,"submitTime":1.457213332141E12,"finishTime":1.457213388105E12,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"siva.anand@gmail.com","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"2bb6d9aa-3ba8-4e01-a400-7ef5e9c49e16"},{"version":"CommandV1","origId":132585,"guid":"cfc3f88c-fa1f-4ef9-9f6f-c44596d2ce1d","subtype":"command","commandType":"auto","position":0.999755859375,"command":"val two_one = matrix(1, 0) // Remember the index starts from zero","commandVersion":0,"state":"finished","results":{"type":"html","data":"<div class=\"ansiout\">two_one: Double = 0.07183200035510628\n</div>","arguments":{},"addedWidgets":{},"removedWidgets":[]},"errorSummary":"java.lang.IndexOutOfBoundsException: (2,1) not in [-2,2) x [-3,3)","error":"<div class=\"ansiout\">\tat breeze.linalg.DenseMatrix.apply(DenseMatrix.scala:83)\n\tat breeze.linalg.DenseMatrix.apply$mcD$sp(DenseMatrix.scala:82)</div>","startTime":1.457213459425E12,"submitTime":1.457213404823E12,"finishTime":1.457213459537E12,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"siva.anand@gmail.com","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"d553ba14-1b3c-4ffa-a5e0-3dad6d91c7d0"},{"version":"CommandV1","origId":132586,"guid":"031bf8cc-86b8-4c9a-948d-401be4531598","subtype":"command","commandType":"auto","position":1.0,"command":"%md\n### Other Manipulation\n\n|Operation                       |Breeze                                   |Matlab              |Numpy                          |R\n|--------------------------------|-----------------------------------------|--------------------|-------------------------------|-------------\n|Reshaping                       |`a.reshape(3, 2)`                        |`reshape(a, 3, 2)`  |`a.reshape(3,2)`               |`matrix(a,nrow=3,byrow=T)`\n|Flatten matrix                  |`a.toDenseVector` (Makes copy)           |`a(:)`              |`a.flatten()`                  |`as.vector(a)`\n|Copy lower triangle             |`lowerTriangular(a)`                     |`tril(a)`           |`tril(a)`                      |`a[upper.tri(a)] <- 0`\n|Copy upper triangle             |`upperTriangular(a)`                     |`triu(a)`           |`triu(a)`                      |`a[lower.tri(a)] <- 0`\n|Copy (note, no parens!!)        |`a.copy`                                 |                    |`np.copy(a)`                   |\n|Create view of matrix diagonal  |`diag(a)`                                |NA                  |`diagonal(a)` (Numpy >= 1.9)   |\n|Vector Assignment to subset     |`a(1 to 4) := 5.0`                       |`a(2:5) = 5`        |`a[1:4] = 5`                   |`a[2:5] = 5`\n|Vector Assignment to subset     |`a(1 to 4) := DenseVector(1.0,2.0,3.0)`  |`a(2:5) = [1 2 3]`  |`a[1:4] = array([1,2,3])`      |`a[2:5] = c(1,2,3)`\n|Matrix Assignment to subset     |`a(1 to 3,1 to 3) := 5.0`                |`a(2:4,2:4) = 5`    |`a[1:3,1:3] = 5`               |`a[2:4,2:4] = 5`\n|Matrix Assignment to column     |`a(::, 2) := 5.0`                        |`a(:,3) = 5`        |`a[:,2] = 5`                   |`a[,3] = 5`\n|Matrix vertical concatenate     |`DenseMatrix.vertcat(a,b)`               |`[a ; b]`           |`vstack((a,b))`                |`rbind(a, b)`\n|Matrix horizontal concatenate   |`DenseMatrix.horzcat(d,e)`               |`[d , e]`           |`hstack((d,e))`                |`cbind(d, e)`\n|Vector concatenate              |`DenseVector.vertcat(a,b)`               |`[a b]`             |`concatenate((a,b))`           |`c(a, b)`\n\n### Operations\n\n|Operation                           |Breeze       |Matlab                                                 |Numpy        |R\n|------------------------------------|-------------|-------------------------------------------------------|-------------|---------------------\n|Elementwise addition                |`a + b`      |`a + b`                                                |`a + b`      |`a + b`\n|Shaped/Matrix multiplication        |`a * b`      |`a * b`                                                |`dot(a, b)`  |`a %*% b`\n|Elementwise multiplication          |`a :* b`     |`a .* b`                                               |`a * b`      |`a * b`\n|Elementwise division                |`a :/ b`     |`a ./ b`                                               |`a / b`      |`a / b`\n|Elementwise comparison              |`a :< b`     |`a < b` (gives matrix of 1/0 instead of true/false)    |`a < b`      |`a < b`\n|Elementwise equals                  |`a :== b`    |`a == b` (gives matrix of 1/0 instead of true/false)   |`a == b`     |`a == b`\n|Inplace addition                    |`a :+= 1.0`  |`a += 1`                                               |`a += 1`     |`a = a + 1`\n|Inplace elementwise multiplication  |`a :*= 2.0`  |`a *= 2`                                               |`a *= 2`     |`a = a * 2`\n|Vector dot product                  |`a dot b`, `a.t * b`<sup>?</sup>    |`dot(a,b)`                      |`dot(a,b)`   |`crossprod(a,b)`\n|Elementwise max                     |`max(a)`     |`max(a)`                                               |`a.max()`    |`max(a)`\n|Elementwise argmax                  |`argmax(a)`  |`[v i] = max(a); i`                                    |`a.argmax()` |`which.max(a)`\n\n\n### Sum\n\n|Operation                                     |Breeze                                |Matlab          |Numpy         |R     \n|----------------------------------------------|--------------------------------------|----------------|--------------|----------\n|Elementwise sum                               |`sum(a)`                              |`sum(sum(a))`   |`a.sum()`     |`sum(a)`\n|Sum down each column (giving a row vector)    |`sum(a, Axis._0)` or `sum(a(::, *))`  |`sum(a)`        |`sum(a,0)`    |`apply(a,2,sum)`\n|Sum across each row (giving a column vector)  |`sum(a, Axis._1)` or `sum(a(*, ::))`  |`sum(a')`       |`sum(a,1)`    |`apply(a,1,sum)`\n|Trace (sum of diagonal elements)              |`trace(a)`                            |`trace(a)`      |`a.trace()`   |`sum(diag(a))`\n|Cumulative sum                                |`accumulate(a)`                       |`cumsum(a)`     |`a.cumsum()`  |`apply(a,2,cumsum)`\n\n### Boolean Operators\n\n|Operation                                     |Breeze                                |Matlab            |Numpy       |R\n|----------------------------------------------|--------------------------------------|------------------|------------|--------\n|Elementwise and                               |`a :& b`                              |`a && b`          |`a & b`     |`a & b`\n|Elementwise or                                |`a :| b`                              |`a || b`          |`a | b`     |`a | b`\n|Elementwise not                               |`!a`                                  |`~a`              |`~a`        |`!a` \n|True if any element is nonzero                |`any(a)`                              |`any(a)`          |any(a)      |\n|True if all elements are nonzero              |`all(a)`                              |`all(a)`          |all(a)      |\n\n### Linear Algebra Functions\n\n|Operation                                     |Breeze                                                       |Matlab            |Numpy               |R\n|----------------------------------------------|-------------------------------------------------------------|------------------|--------------------|-----------------\n|Linear solve                                  |`a \\ b`                                                      |`a \\ b`           |`linalg.solve(a,b)` |`solve(a,b)`\n|Transpose                                     |`a.t`                                                        |`a'`              |`a.conj.transpose()`|`t(a)`\n|Determinant                                   |`det(a)`                                                     |`det(a)`          |`linalg.det(a)`     |`det(a)`\n|Inverse                                       |`inv(a)`                                                     |`inv(a)`          |`linalg.inv(a)`     |`solve(a)`\n|Moore-Penrose Pseudoinverse                   |`pinv(a)`                                                    |`pinv(a)`         |`linalg.pinv(a)`    |\n|Vector Frobenius Norm                         |`norm(a)`                                                    |`norm(a)`         |`norm(a)`           |\n|Eigenvalues (Symmetric)                       |`eigSym(a)`                                                  |`[v,l] = eig(a)`  |`linalg.eig(a)[0]`  |\n|Eigenvalues                                   |`val (er, ei, _) = eig(a)` (separate real & imaginary part)  |`eig(a)`          |`linalg.eig(a)[0]`  |`eigen(a)$values`\n|Eigenvectors                                  |`eig(a)._3`                                                  |`[v,l] = eig(a)`  |`linalg.eig(a)[1]`  |`eigen(a)$vectors`\n|Singular Value Decomposition                  |`val svd.SVD(u,s,v) = svd(a)`                                |`svd(a)`          |`linalg.svd(a)`     |`svd(a)$d`\n|Rank                                          |`rank(a)`                                                    |`rank(a)`         |`rank(a)`           |`rank(a)`\n|Vector length                                 |`a.length`                                                   |`size(a)`         |`a.size`            |`length(a)`\n|Matrix rows                                   |`a.rows`                                                     |`size(a,1)`       |`a.shape[0]`        |`nrow(a)`\n|Matrix columns                                |`a.cols`                                                     |`size(a,2)`       |`a.shape[1]`        |`ncol(a)`\n\n### Rounding and Signs\n\n|Operation                           |Breeze       |Matlab          |Numpy        |R\n|------------------------------------|-------------|----------------|-------------|--------------\n|Round                               |`round(a)`   |`round(a)`      |`around(a)`  |`round(a)`\n|Ceiling                             |`ceil(a)`    |`ceil(a)`       |`ceil(a)`    |`ceiling(a)`\n|Floor                               |`floor(a)`   |`floor(a)`      |`floor(a)`   |`floor(a)`\n|Sign                                |`signum(a)`  |`sign(a)`       |`sign(a)`    |`sign(a)`\n|Absolute Value                      |`abs(a)`     |`abs(a)`        |`abs(a)`     |`abs(a)`\n\n### Constants\n\n|Operation     |Breeze          |Matlab    |Numpy     |R\n|--------------|----------------|----------|----------|-----------\n|Not a Number  |`NaN` or `nan`  |`NaN`     |`nan`     |`NA`\n|Infinity      |`Inf` or `inf`  |`Inf`     |`inf`     |`Inf`\n|Pi            |`Constants.Pi`  |`pi`      |`math.pi` |`pi`\n|e             |`Constants.E`   |`exp(1)`  |`math.e`  |`exp(1)`\n\n\n## Complex numbers\n\nIf you make use of complex numbers, you will want to include a\n`breeze.math._` import. This declares a `i` variable, and provides\nimplicit conversions from Scala?s basic types to complex types.\n\n|Operation          |Breeze                       |Matlab     |Numpy                        |R\n|-------------------|-----------------------------|-----------|-----------------------------|------------\n|Imaginary unit     |`i`                          |`i`        |`z = 1j`                     |`1i`\n|Complex numbers    |`3 + 4 * i` or `Complex(3,4)`|`3 + 4i`   |`z = 3 + 4j`                 |`3 + 4i`\n|Absolute Value     |`abs(z)` or `z.abs`          |`abs(z)`   |`abs(z)`                     |`abs(z)`\n|Real Component     |`z.real`                     |`real(z)`  |`z.real`                     |`Re(z)`\n|Imaginary Component|`z.imag`                     |`imag(z)`  |`z.imag()`                   |`Im(z)`\n|Imaginary Conjugate|`z.conjugate`                |`conj(z)`  |`z.conj()` or `z.conjugate()`|`Conj(z)`\n\n## Numeric functions\n\nBreeze contains a fairly comprehensive set of special functions under\nthe `breeze.numerics._` import. These functions can be applied to single\nelements, vectors or matrices of Doubles. This includes versions of the\nspecial functions from `scala.math` that can be applied to vectors and\nmatrices. Any function acting on a basic numeric type can ?vectorized?,\nto a [[UFunc|Universal Functions]] function, which can act elementwise on vectors and matrices:\n```scala\nval v = DenseVector(1.0,2.0,3.0)\nexp(v) // == DenseVector(2.7182818284590455, 7.38905609893065, 20.085536923187668)\n```\n\nUFuncs can also be used in-place on Vectors and Matrices:\n```scala\nval v = DenseVector(1.0,2.0,3.0)\nexp.inPlace(v) // == DenseVector(2.7182818284590455, 7.38905609893065, 20.085536923187668)\n```\n\nSee [[Universal Functions]] for more information.\n\nHere is a (non-exhaustive) list of UFuncs in Breeze:\n\n### Trigonometry\n* `sin`, `sinh`, `asin`, `asinh`\n* `cos`, `cosh`, `acos`, `acosh`\n* `tan`, `tanh`, `atan`, `atanh`\n* `atan2`\n* `sinc(x) == sin(x)/x`\n* `sincpi(x) == sinc(x * Pi)`\n\n### Logarithm, Roots, and Exponentials\n* `log`, `exp` `log10` \n* `log1p`, `expm1`\n* `sqrt`, `sbrt`\n* `pow`\n\n### Gamma Function and its cousins\n\nThe [gamma function](http://en.wikipedia.org/wiki/Gamma_function) is the extension of the factorial function to the reals.\nNumpy needs `from scipy.special import *` for this and subsequent sections.\n\n|Operation                           |Breeze              |Matlab                  |Numpy                   |R\n|------------------------------------|--------------------|------------------------|------------------------|----------------\n|Gamma function                      |`exp(lgamma(a))`    |`gamma(a)`              |`gamma(a)`              |`gamma(a)`  \n|log Gamma function                  |`lgamma(a)`         |`gammaln(a)`            |`gammaln(a)`            |`lgamma(a)`\n|Incomplete gamma function           |`gammp(a, x)`       |`gammainc(a, x)`        |`gammainc(a, x)`        |`pgamma(a, x)` (requires stats library)\n|Upper incomplete gamma function     |`gammq(a, x)`       |`gammainc(a, x, tail)`  |`gammaincc(a, x)`       |`pgamma(x, a, lower = FALSE) * gamma(a)` (requires stats library)\n|derivative of lgamma                |`digamma(a)`        |`psi(a)`                |`polygamma(0, a)`       |`digamma(a)`\n|derivative of digamma               |`trigamma(a)`       |`psi(1, a)`             |`polygamma(1, a)`       |`trigama(a)`\n|nth derivative of digamma           | na                 |`psi(n, a)`             |`polygamma(n, a)`       |`psigamma(a, deriv = n)`\n|Log [Beta function](http://en.wikipedia.org/wiki/Beta_function)| lbeta(a,b)  |`betaln(a, b)` |`betaln(a,b)`|`lbeta(a, b)`\n|Generalized Log [Beta function](http://en.wikipedia.org/wiki/Beta_function)| lbeta(a)  | na|na             |     \n\n### Error Function\n\nThe [error function](http://en.wikipedia.org/wiki/Error_function)...\n\n|Operation                           |Breeze           |Matlab          |Numpy                |R\n|------------------------------------|-----------------|----------------|---------------------|-------------\n| error function                     |`erf(a)`         |`erf(a)`        |`erf(a)`             |`2 * pnorm(a * sqrt(2)) - 1`\n| 1 - erf(a)                         |`erfc(a)`        |`erfc(a)`       |`erfc(a)`            |`2 * pnorm(a * sqrt(2), lower = FALSE)`\n| inverse error function             |`erfinv(a)`      |`erfinv(a)`     |`erfinv(a)`          |`qnorm((1 + a) / 2) / sqrt(2)`\n| inverse erfc                       |`erfcinv(a)`     |`erfcinv(a)`    |`erfcinv(a)`         |`qnorm(a / 2, lower = FALSE) / sqrt(2)`\n\n### Other functions\n\n|Operation                           |Breeze           |Matlab          |Numpy                |R\n|------------------------------------|-----------------|----------------|---------------------|------------\n| logistic sigmoid                   |`sigmoid(a)`     | na             | `expit(a)`          |`sigmoid(a)` (requires pracma library) \n| Indicator function                 |`I(a)`           | not needed     | `where(cond, 1, 0)` |`0 + (a > 0)`\n| Polynominal evaluation             |`polyval(coef,x)`|                |                     | \n\n### Map and Reduce\n\nFor most simple mapping tasks, one can simply use vectorized, or universal functions. \nGiven a vector `v`, we can simply take the log of each element of a vector with `log(v)`.\nSometimes, however, we want to apply a somewhat idiosyncratic function to each element of a vector.\nFor this, we can use the map function:\n\n```scala\nval v = DenseVector(1.0,2.0,3.0)\nv.map( xi => foobar(xi) )\n``` \n\nBreeze provides a number of built in reduction functions such as sum, mean.\nYou can implement a custom reduction using the higher order function `reduce`.\nFor instance, we can sum the first 9 integers as follows:\n\n```scala\nval v = linspace(0,9,10)\nval s = v.reduce( _ + _ )\n```\n\n## Broadcasting\n\nSometimes we want to apply an operation to every row or column of a\nmatrix, as a unit. For instance, you might want to compute the mean of\neach row, or add a vector to every column. Adapting a matrix so that\noperations can be applied columnwise or rowwise is called\n**broadcasting**. Languages like R and numpy automatically and\nimplicitly do broadcasting, meaning they won?t stop you if you\naccidentally add a matrix and a vector. In Breeze, you have to signal\nyour intent using the broadcasting operator `*`. The `*` is meant to\nevoke ?foreach? visually. Here are some examples:\n\n```scala\n    val dm = DenseMatrix((1.0,2.0,3.0),\n                         (4.0,5.0,6.0))\n\n    val res = dm(::, *) + DenseVector(3.0, 4.0)\n    assert(res === DenseMatrix((4.0, 5.0, 6.0), (8.0, 9.0, 10.0)))\n\n    res(::, *) := DenseVector(3.0, 4.0)\n    assert(res === DenseMatrix((3.0, 3.0, 3.0), (4.0, 4.0, 4.0)))\n\n    val m = DenseMatrix((1.0, 3.0), (4.0, 4.0))\n    // unbroadcasted sums all elements\n    assert(sum(m) === 12.0)\n    assert(mean(m) === 3.0)\n\n    assert(sum(m(*, ::)) === DenseVector(4.0, 8.0))\n    assert(sum(m(::, *)) === DenseMatrix((5.0, 7.0)))\n\n    assert(mean(m(*, ::)) === DenseVector(2.0, 4.0))\n    assert(mean(m(::, *)) === DenseMatrix((2.5, 3.5)))\n\n\n```\n\nThe UFunc trait is similar to numpy?s ufunc.  See [[Universal Functions]] for more information on Breeze UFuncs.\n\n## Casting and type safety\n\nCompared to Numpy and Matlab, Breeze requires you to be more explicit about the types of your variables. When you create a new vector for example, you must specify a type (such as in `DenseVector.zeros[Double](n)`) in cases where a type can not be inferred automatically. Automatic inference will occur when you create a vector by passing its initial values in (`DenseVector`). A common mistake is using integers for initialisation (e.g. `DenseVector`), which would give a matrix of integers instead of doubles. Both Numpy and Matlab would default to doubles instead.\n\nBreeze will not convert integers to doubles for you in most expressions. Simple operations like `a :+\n3` when `a` is a `DenseVector[Double]` will not compile. Breeze provides a convert function, which can be used to explicitly cast. You can also use `v.mapValues(_.toDouble)`.\n\n### Casting\n\n| Operation                       | Breeze                                        | Matlab              | Numpy           | R\n| ------------------------------- | --------------------------------------------- | ------------------- | ----------------|--------------------\n| Convert to Int                  | `convert(a, Int)`                             | `int(a)`            | `a.astype(int)` |`as.integer(a)`\n\n## Performance\n\nBreeze uses [netlib-java](https://github.com/fommil/netlib-java/) for\nits core linear algebra routines. This includes all the cubic time\noperations, matrix-matrix and matrix-vector multiplication. Special\nefforts are taken to ensure that arrays are not copied.\n\nNetlib-java will attempt to load system optimised BLAS/LAPACK if they\nare installed, falling back to the reference natives, falling back to\npure Java. Set your logger settings to `ALL` for the\n`com.github.fommil.netlib` package to check the status, and to\n`com.github.fommil.jniloader` for a more detailed breakdown. Read the\nnetlib-java project page for more details.\n\nCurrently vectors and matrices over types other than `Double`, `Float`\nand `Int` are boxed, so they will typically be a lot slower. If you find\nyourself needing other AnyVal types like `Long` or `Short`, please ask\non the list about possibly adding support for them.","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"856e8135-f82d-46e3-b985-ab771f52c9d0"},{"version":"CommandV1","origId":132587,"guid":"9acf003d-27db-4623-abbf-7d18d4a008bb","subtype":"command","commandType":"auto","position":2.0,"command":"%md\n\n# [Scalable Data Science](http://www.math.canterbury.ac.nz/~r.sainudiin/courses/ScalableDataScience/)\n\n\n### prepared by [Raazesh Sainudiin](https://nz.linkedin.com/in/raazesh-sainudiin-45955845) and [Sivanand Sivaram](https://www.linkedin.com/in/sivanand)\n\n*supported by* [![](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/images/databricks_logoTM_200px.png)](https://databricks.com/)\nand \n[![](https://raw.githubusercontent.com/raazesh-sainudiin/scalable-data-science/master/images/AWS_logoTM_200px.png)](https://www.awseducate.com/microsite/CommunitiesEngageHome)","commandVersion":0,"state":"finished","results":null,"errorSummary":null,"error":null,"startTime":0.0,"submitTime":0.0,"finishTime":0.0,"collapsed":false,"bindings":{},"inputWidgets":{},"displayType":"table","width":"auto","height":"auto","xColumns":null,"yColumns":null,"pivotColumns":null,"pivotAggregation":null,"customPlotOptions":{},"commentThread":[],"commentsVisible":false,"parentHierarchy":[],"diffInserts":[],"diffDeletes":[],"globalVars":{},"latestUser":"","commandTitle":"","showCommandTitle":false,"hideCommandCode":false,"hideCommandResult":false,"iPythonMetadata":null,"nuid":"ac30606b-dbc4-438b-9609-5b244d488794"}],"dashboards":[],"guid":"7b370137-cffd-473b-8f3c-bfdb0ccb0462","globalVars":{},"iPythonMetadata":null,"inputWidgets":{}};</script>
<script
 src="https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/js/notebook-main.js"
 onerror="window.mainJsLoadError = true;"></script>
</head>
<body>
  <script>
if (window.mainJsLoadError) {
  var u = 'https://databricks-prod-cloudfront.cloud.databricks.com/static/201602081754420800-0c2673ac858e227cad536fdb45d140aeded238db/js/notebook-main.js';
  var b = document.getElementsByTagName('body')[0];
  var c = document.createElement('div');
  c.innerHTML = ('<h1>Network Error</h1>' +
    '<p><b>Please check your network connection and try again.</b></p>' +
    '<p>Could not load a required resource: ' + u + '</p>');
  c.style.margin = '30px';
  c.style.padding = '20px 50px';
  c.style.backgroundColor = '#f5f5f5';
  c.style.borderRadius = '5px';
  b.appendChild(c);
}
</script>
</body>
</html>