Sophie

Sophie

distrib > Fedora > 20 > x86_64 > by-pkgid > d9f573299e87e886807be879704f0b6e > files > 117

julia-doc-0.3.4-1.fc20.noarch.rpm




<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Multi-dimensional Arrays &mdash; Julia Language 0.3.4 documentation</title>
  

  
  

  
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>

  
  
    

  

  
  
    <link rel="stylesheet" href="../_static/julia.css" type="text/css" />
  

  
    <link rel="stylesheet" href="../_static/julia.css" type="text/css" />
  
    <link rel="top" title="Julia Language 0.3.4 documentation" href="../index.html"/>
        <link rel="next" title="Linear algebra" href="linear-algebra.html"/>
        <link rel="prev" title="Metaprogramming" href="metaprogramming.html"/> 

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-nav-search">
        <a href="http://julialang.org/"><img src="../_static/julia-logo.svg" class="logo"></a>
        <!--
        <a href="../index.html" class="fa fa-home"> Julia Language</a>
        -->
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
        
        
            <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="introduction.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="getting-started.html">Getting Started</a><ul>
<li class="toctree-l2"><a class="reference internal" href="getting-started.html#resources">Resources</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="variables.html">Variables</a><ul>
<li class="toctree-l2"><a class="reference internal" href="variables.html#allowed-variable-names">Allowed Variable Names</a></li>
<li class="toctree-l2"><a class="reference internal" href="variables.html#stylistic-conventions">Stylistic Conventions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="integers-and-floating-point-numbers.html">Integers and Floating-Point Numbers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#integers">Integers</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#floating-point-numbers">Floating-Point Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#arbitrary-precision-arithmetic">Arbitrary Precision Arithmetic</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#numeric-literal-coefficients">Numeric Literal Coefficients</a></li>
<li class="toctree-l2"><a class="reference internal" href="integers-and-floating-point-numbers.html#literal-zero-and-one">Literal zero and one</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="mathematical-operations.html">Mathematical Operations and Elementary Functions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#arithmetic-operators">Arithmetic Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#bitwise-operators">Bitwise Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#updating-operators">Updating operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#numeric-comparisons">Numeric Comparisons</a></li>
<li class="toctree-l2"><a class="reference internal" href="mathematical-operations.html#elementary-functions">Elementary Functions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="complex-and-rational-numbers.html">Complex and Rational Numbers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="complex-and-rational-numbers.html#complex-numbers">Complex Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="complex-and-rational-numbers.html#rational-numbers">Rational Numbers</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="strings.html">Strings</a><ul>
<li class="toctree-l2"><a class="reference internal" href="strings.html#characters">Characters</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#string-basics">String Basics</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#unicode-and-utf-8">Unicode and UTF-8</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#interpolation">Interpolation</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#common-operations">Common Operations</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#non-standard-string-literals">Non-Standard String Literals</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#regular-expressions">Regular Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#id3">Byte Array Literals</a></li>
<li class="toctree-l2"><a class="reference internal" href="strings.html#version-number-literals">Version Number Literals</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="functions.html">Functions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="functions.html#argument-passing-behavior">Argument Passing Behavior</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#the-return-keyword">The <tt class="docutils literal"><span class="pre">return</span></tt> Keyword</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#id1">Operators Are Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#operators-with-special-names">Operators With Special Names</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#anonymous-functions">Anonymous Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#multiple-return-values">Multiple Return Values</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#varargs-functions">Varargs Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#optional-arguments">Optional Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#keyword-arguments">Keyword Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#evaluation-scope-of-default-values">Evaluation Scope of Default Values</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#block-syntax-for-function-arguments">Block Syntax for Function Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="functions.html#further-reading">Further Reading</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="control-flow.html">Control Flow</a><ul>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#compound-expressions">Compound Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#conditional-evaluation">Conditional Evaluation</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#short-circuit-evaluation">Short-Circuit Evaluation</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#repeated-evaluation-loops">Repeated Evaluation: Loops</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#exception-handling">Exception Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="control-flow.html#tasks-aka-coroutines">Tasks (aka Coroutines)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="variables-and-scoping.html">Scope of Variables</a><ul>
<li class="toctree-l2"><a class="reference internal" href="variables-and-scoping.html#for-loops-and-comprehensions">For Loops and Comprehensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="variables-and-scoping.html#constants">Constants</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="types.html">Types</a><ul>
<li class="toctree-l2"><a class="reference internal" href="types.html#type-declarations">Type Declarations</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#abstract-types">Abstract Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#bits-types">Bits Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#composite-types">Composite Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#immutable-composite-types">Immutable Composite Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#declared-types">Declared Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#tuple-types">Tuple Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#type-unions">Type Unions</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#man-parametric-types">Parametric Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#type-aliases">Type Aliases</a></li>
<li class="toctree-l2"><a class="reference internal" href="types.html#operations-on-types">Operations on Types</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="methods.html">Methods</a><ul>
<li class="toctree-l2"><a class="reference internal" href="methods.html#defining-methods">Defining Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="methods.html#method-ambiguities">Method Ambiguities</a></li>
<li class="toctree-l2"><a class="reference internal" href="methods.html#parametric-methods">Parametric Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="methods.html#note-on-optional-and-keyword-arguments">Note on Optional and keyword Arguments</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="constructors.html">Constructors</a><ul>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#outer-constructor-methods">Outer Constructor Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#inner-constructor-methods">Inner Constructor Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#incomplete-initialization">Incomplete Initialization</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#parametric-constructors">Parametric Constructors</a></li>
<li class="toctree-l2"><a class="reference internal" href="constructors.html#case-study-rational">Case Study: Rational</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="conversion-and-promotion.html">Conversion and Promotion</a><ul>
<li class="toctree-l2"><a class="reference internal" href="conversion-and-promotion.html#conversion">Conversion</a></li>
<li class="toctree-l2"><a class="reference internal" href="conversion-and-promotion.html#promotion">Promotion</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="modules.html">Modules</a><ul>
<li class="toctree-l2"><a class="reference internal" href="modules.html#summary-of-module-usage">Summary of module usage</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="metaprogramming.html">Metaprogramming</a><ul>
<li class="toctree-l2"><a class="reference internal" href="metaprogramming.html#expressions-and-eval">Expressions and Eval</a></li>
<li class="toctree-l2"><a class="reference internal" href="metaprogramming.html#macros">Macros</a></li>
<li class="toctree-l2"><a class="reference internal" href="metaprogramming.html#reflection">Reflection</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Multi-dimensional Arrays</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#arrays">Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="#sparse-matrices">Sparse Matrices</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="linear-algebra.html">Linear algebra</a><ul>
<li class="toctree-l2"><a class="reference internal" href="linear-algebra.html#matrix-factorizations">Matrix factorizations</a></li>
<li class="toctree-l2"><a class="reference internal" href="linear-algebra.html#special-matrices">Special matrices</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="networking-and-streams.html">Networking and Streams</a><ul>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#basic-stream-i-o">Basic Stream I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#text-i-o">Text I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#working-with-files">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#a-simple-tcp-example">A simple TCP example</a></li>
<li class="toctree-l2"><a class="reference internal" href="networking-and-streams.html#resolving-ip-addresses">Resolving IP Addresses</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="parallel-computing.html">Parallel Computing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#data-movement">Data Movement</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#parallel-map-and-loops">Parallel Map and Loops</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#synchronization-with-remote-references">Synchronization With Remote References</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#scheduling">Scheduling</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#distributed-arrays">Distributed Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#constructing-distributed-arrays">Constructing Distributed Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#distributed-array-operations">Distributed Array Operations</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#shared-arrays-experimental">Shared Arrays (Experimental)</a></li>
<li class="toctree-l2"><a class="reference internal" href="parallel-computing.html#clustermanagers">ClusterManagers</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="running-external-programs.html">Running External Programs</a><ul>
<li class="toctree-l2"><a class="reference internal" href="running-external-programs.html#interpolation">Interpolation</a></li>
<li class="toctree-l2"><a class="reference internal" href="running-external-programs.html#quoting">Quoting</a></li>
<li class="toctree-l2"><a class="reference internal" href="running-external-programs.html#pipelines">Pipelines</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="calling-c-and-fortran-code.html">Calling C and Fortran Code</a><ul>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#mapping-c-types-to-julia">Mapping C Types to Julia</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#accessing-data-through-a-pointer">Accessing Data through a Pointer</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#passing-pointers-for-modifying-inputs">Passing Pointers for Modifying Inputs</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#garbage-collection-safety">Garbage Collection Safety</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#non-constant-function-specifications">Non-constant Function Specifications</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#indirect-calls">Indirect Calls</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#calling-convention">Calling Convention</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#accessing-global-variables">Accessing Global Variables</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#passing-julia-callback-functions-to-c">Passing Julia Callback Functions to C</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#c">C++</a></li>
<li class="toctree-l2"><a class="reference internal" href="calling-c-and-fortran-code.html#handling-platform-variations">Handling Platform Variations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="interacting-with-julia.html">Interacting With Julia</a><ul>
<li class="toctree-l2"><a class="reference internal" href="interacting-with-julia.html#the-different-prompt-modes">The different prompt modes</a></li>
<li class="toctree-l2"><a class="reference internal" href="interacting-with-julia.html#key-bindings">Key bindings</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="embedding.html">Embedding Julia</a><ul>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#high-level-embedding">High-Level Embedding</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#converting-types">Converting Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#calling-julia-functions">Calling Julia Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#memory-management">Memory Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#working-with-arrays">Working with Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="embedding.html#exceptions">Exceptions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="packages.html">Packages</a><ul>
<li class="toctree-l2"><a class="reference internal" href="packages.html#package-status">Package Status</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#adding-and-removing-packages">Adding and Removing Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#installing-unregistered-packages">Installing Unregistered Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#updating-packages">Updating Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#checkout-pin-and-free">Checkout, Pin and Free</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="packages.html#package-development">Package Development</a><ul>
<li class="toctree-l2"><a class="reference internal" href="packages.html#initial-setup">Initial Setup</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#generating-a-new-package">Generating a New Package</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#making-your-package-available">Making Your Package Available</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#publishing-your-package">Publishing Your Package</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#tagging-package-versions">Tagging Package Versions</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#fixing-package-requirements">Fixing Package Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="packages.html#man-package-requirements">Requirements Specification</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="performance-tips.html">Performance Tips</a><ul>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-global-variables">Avoid global variables</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#measure-performance-with-time-and-pay-attention-to-memory-allocation">Measure performance with <tt class="docutils literal"><span class="pre">&#64;time</span></tt> and pay attention to memory allocation</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#tools">Tools</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-containers-with-abstract-type-parameters">Avoid containers with abstract type parameters</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#type-declarations">Type declarations</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#break-functions-into-multiple-definitions">Break functions into multiple definitions</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#write-type-stable-functions">Write &#8220;type-stable&#8221; functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-changing-the-type-of-a-variable">Avoid changing the type of a variable</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#separate-kernel-functions">Separate kernel functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#access-arrays-in-memory-order-along-columns">Access arrays in memory order, along columns</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#pre-allocating-outputs">Pre-allocating outputs</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#avoid-string-interpolation-for-i-o">Avoid string interpolation for I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#fix-deprecation-warnings">Fix deprecation warnings</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#tweaks">Tweaks</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance-tips.html#performance-annotations">Performance Annotations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="style-guide.html">Style Guide</a><ul>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#write-functions-not-just-scripts">Write functions, not just scripts</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-writing-overly-specific-types">Avoid writing overly-specific types</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#handle-excess-argument-diversity-in-the-caller">Handle excess argument diversity in the caller</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#append-to-names-of-functions-that-modify-their-arguments">Append <cite>!</cite> to names of functions that modify their arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-strange-type-unions">Avoid strange type Unions</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#try-to-avoid-nullable-fields">Try to avoid nullable fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-elaborate-container-types">Avoid elaborate container types</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-underscores-in-names">Avoid underscores in names</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overuse-try-catch">Don&#8217;t overuse try-catch</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-parenthesize-conditions">Don&#8217;t parenthesize conditions</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overuse">Don&#8217;t overuse ...</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-use-unnecessary-static-parameters">Don&#8217;t use unnecessary static parameters</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#avoid-confusion-about-whether-something-is-an-instance-or-a-type">Avoid confusion about whether something is an instance or a type</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overuse-macros">Don&#8217;t overuse macros</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-expose-unsafe-operations-at-the-interface-level">Don&#8217;t expose unsafe operations at the interface level</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#don-t-overload-methods-of-base-container-types">Don&#8217;t overload methods of base container types</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#be-careful-with-type-equality">Be careful with type equality</a></li>
<li class="toctree-l2"><a class="reference internal" href="style-guide.html#do-not-write-x-f-x">Do not write <tt class="docutils literal"><span class="pre">x-&gt;f(x)</span></tt></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="faq.html#sessions-and-the-repl">Sessions and the REPL</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#functions">Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#types-type-declarations-and-constructors">Types, type declarations, and constructors</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#nothingness-and-missing-values">Nothingness and missing values</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#julia-releases">Julia Releases</a></li>
<li class="toctree-l2"><a class="reference internal" href="faq.html#developing-julia">Developing Julia</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="noteworthy-differences.html">Noteworthy Differences from other Languages</a><ul>
<li class="toctree-l2"><a class="reference internal" href="noteworthy-differences.html#noteworthy-differences-from-matlab">Noteworthy differences from MATLAB</a></li>
<li class="toctree-l2"><a class="reference internal" href="noteworthy-differences.html#noteworthy-differences-from-r">Noteworthy differences from R</a></li>
<li class="toctree-l2"><a class="reference internal" href="noteworthy-differences.html#noteworthy-differences-from-python">Noteworthy differences from Python</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/base.html">The Standard Library</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#introduction">Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#getting-around">Getting Around</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#all-objects">All Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#types">Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#generic-functions">Generic Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#syntax">Syntax</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#iteration">Iteration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#general-collections">General Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#iterable-collections">Iterable Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#indexable-collections">Indexable Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#associative-collections">Associative Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#set-like-collections">Set-Like Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#dequeues">Dequeues</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#strings">Strings</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#i-o">I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#network-i-o">Network I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#text-i-o">Text I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#multimedia-i-o">Multimedia I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#memory-mapped-i-o">Memory-mapped I/O</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#standard-numeric-types">Standard Numeric Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#mathematical-operators">Mathematical Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#mathematical-functions">Mathematical Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#data-formats">Data Formats</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#numbers">Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#bigfloats">BigFloats</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#random-numbers">Random Numbers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#arrays">Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#combinatorics">Combinatorics</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#statistics">Statistics</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#signal-processing">Signal Processing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#numerical-integration">Numerical Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#parallel-computing">Parallel Computing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#distributed-arrays">Distributed Arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#shared-arrays-experimental-unix-only-feature">Shared Arrays (Experimental, UNIX-only feature)</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#system">System</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#c-interface">C Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#errors">Errors</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#tasks">Tasks</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#events">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#reflection">Reflection</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/base.html#internals">Internals</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/sparse.html">Sparse Matrices</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/linalg.html">Linear Algebra</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/linalg.html#module-Base.LinAlg.BLAS">BLAS Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/constants.html">Constants</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/file.html">Filesystem</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/punctuation.html">Punctuation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/sort.html">Sorting and Related Functions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/sort.html#sorting-functions">Sorting Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/sort.html#order-related-functions">Order-Related Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/sort.html#sorting-algorithms">Sorting Algorithms</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/pkg.html">Package Manager Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/collections.html">Collections and Data Structures</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/collections.html#priorityqueue">PriorityQueue</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/collections.html#heap-functions">Heap Functions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/graphics.html">Graphics</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/graphics.html#geometry">Geometry</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/test.html">Unit and Functional Testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#overview">Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#handlers">Handlers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#macros">Macros</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/test.html#functions">Functions</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/test.html#testing-base-julia">Testing Base Julia</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stdlib/profile.html">Profiling</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#basic-usage">Basic usage</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#accumulation-and-clearing">Accumulation and clearing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#options-for-controlling-the-display-of-profile-results">Options for controlling the display of profile results</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#configuration">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../stdlib/profile.html#function-reference">Function reference</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../devdocs/julia.html">Documentation of Julia&#8217;s Internals</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/cartesian.html">Base.Cartesian</a></li>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/sysimg.html">System Image Building</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../devdocs/C.html">Developing/debugging Julia&#8217;s C code</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/backtraces.html">Reporting and analyzing crashes (segfaults)</a></li>
<li class="toctree-l2"><a class="reference internal" href="../devdocs/debuggingtips.html">gdb debugging tips</a></li>
</ul>
</li>
</ul>

        
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../index.html">Julia Language</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../index.html">Docs</a> &raquo;</li>
      
    <li>Multi-dimensional Arrays</li>
      <li class="wy-breadcrumbs-aside">
        
          <a href="../_sources/manual/arrays.txt" rel="nofollow"> View page source</a>
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document">
            
  <div class="section" id="multi-dimensional-arrays">
<span id="man-arrays"></span><h1>Multi-dimensional Arrays<a class="headerlink" href="#multi-dimensional-arrays" title="Permalink to this headline">¶</a></h1>
<p>Julia, like most technical computing languages, provides a first-class
array implementation. Most technical computing languages pay a lot of
attention to their array implementation at the expense of other
containers. Julia does not treat arrays in any special way. The array
library is implemented almost completely in Julia itself, and derives
its performance from the compiler, just like any other code written in
Julia.</p>
<p>An array is a collection of objects stored in a multi-dimensional
grid.  In the most general case, an array may contain objects of type
<tt class="docutils literal"><span class="pre">Any</span></tt>.  For most computational purposes, arrays should contain
objects of a more specific type, such as <tt class="docutils literal"><span class="pre">Float64</span></tt> or <tt class="docutils literal"><span class="pre">Int32</span></tt>.</p>
<p>In general, unlike many other technical computing languages, Julia does
not expect programs to be written in a vectorized style for performance.
Julia&#8217;s compiler uses type inference and generates optimized code for
scalar array indexing, allowing programs to be written in a style that
is convenient and readable, without sacrificing performance, and using
less memory at times.</p>
<p>In Julia, all arguments to functions are passed by reference. Some
technical computing languages pass arrays by value, and this is
convenient in many cases. In Julia, modifications made to input arrays
within a function will be visible in the parent function. The entire
Julia array library ensures that inputs are not modified by library
functions. User code, if it needs to exhibit similar behaviour, should
take care to create a copy of inputs that it may modify.</p>
<div class="section" id="arrays">
<h2>Arrays<a class="headerlink" href="#arrays" title="Permalink to this headline">¶</a></h2>
<div class="section" id="basic-functions">
<h3>Basic Functions<a class="headerlink" href="#basic-functions" title="Permalink to this headline">¶</a></h3>
<table border="1" class="docutils">
<colgroup>
<col width="26%" />
<col width="74%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Function</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.eltype" title="Base.eltype"><tt class="xref jl jl-func docutils literal"><span class="pre">eltype(A)</span></tt></a></td>
<td>the type of the elements contained in A</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.length" title="Base.length"><tt class="xref jl jl-func docutils literal"><span class="pre">length(A)</span></tt></a></td>
<td>the number of elements in A</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.ndims" title="Base.ndims"><tt class="xref jl jl-func docutils literal"><span class="pre">ndims(A)</span></tt></a></td>
<td>the number of dimensions of A</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.size" title="Base.size"><tt class="xref jl jl-func docutils literal"><span class="pre">size(A)</span></tt></a></td>
<td>a tuple containing the dimensions of A</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.size" title="Base.size"><tt class="xref jl jl-func docutils literal"><span class="pre">size(A,n)</span></tt></a></td>
<td>the size of A in a particular dimension</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.stride" title="Base.stride"><tt class="xref jl jl-func docutils literal"><span class="pre">stride(A,k)</span></tt></a></td>
<td>the stride (linear index distance between adjacent elements) along dimension k</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.strides" title="Base.strides"><tt class="xref jl jl-func docutils literal"><span class="pre">strides(A)</span></tt></a></td>
<td>a tuple of the strides in each dimension</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="construction-and-initialization">
<h3>Construction and Initialization<a class="headerlink" href="#construction-and-initialization" title="Permalink to this headline">¶</a></h3>
<p>Many functions for constructing and initializing arrays are provided. In
the following list of such functions, calls with a <tt class="docutils literal"><span class="pre">dims...</span></tt> argument
can either take a single tuple of dimension sizes or a series of
dimension sizes passed as a variable number of arguments.</p>
<table border="1" class="docutils">
<colgroup>
<col width="43%" />
<col width="58%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Function</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.Array" title="Base.Array"><tt class="xref jl jl-func docutils literal"><span class="pre">Array(type,</span> <span class="pre">dims...)</span></tt></a></td>
<td>an uninitialized dense array</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.cell" title="Base.cell"><tt class="xref jl jl-func docutils literal"><span class="pre">cell(dims...)</span></tt></a></td>
<td>an uninitialized cell array (heterogeneous array)</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.zeros" title="Base.zeros"><tt class="xref jl jl-func docutils literal"><span class="pre">zeros(type,</span> <span class="pre">dims...)</span></tt></a></td>
<td>an array of all zeros of specified type, defaults to <tt class="docutils literal"><span class="pre">Float64</span></tt> if
<tt class="docutils literal"><span class="pre">type</span></tt> not specified</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.zeros" title="Base.zeros"><tt class="xref jl jl-func docutils literal"><span class="pre">zeros(A)</span></tt></a></td>
<td>an array of all zeros of same element type and shape of <tt class="docutils literal"><span class="pre">A</span></tt></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.ones" title="Base.ones"><tt class="xref jl jl-func docutils literal"><span class="pre">ones(type,</span> <span class="pre">dims...)</span></tt></a></td>
<td>an array of all ones of specified type, defaults to <tt class="docutils literal"><span class="pre">Float64</span></tt> if
<tt class="docutils literal"><span class="pre">type</span></tt> not specified</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.ones" title="Base.ones"><tt class="xref jl jl-func docutils literal"><span class="pre">ones(A)</span></tt></a></td>
<td>an array of all ones of same element type and shape of <tt class="docutils literal"><span class="pre">A</span></tt></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.trues" title="Base.trues"><tt class="xref jl jl-func docutils literal"><span class="pre">trues(dims...)</span></tt></a></td>
<td>a <tt class="docutils literal"><span class="pre">Bool</span></tt> array with all values <tt class="docutils literal"><span class="pre">true</span></tt></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.falses" title="Base.falses"><tt class="xref jl jl-func docutils literal"><span class="pre">falses(dims...)</span></tt></a></td>
<td>a <tt class="docutils literal"><span class="pre">Bool</span></tt> array with all values <tt class="docutils literal"><span class="pre">false</span></tt></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.reshape" title="Base.reshape"><tt class="xref jl jl-func docutils literal"><span class="pre">reshape(A,</span> <span class="pre">dims...)</span></tt></a></td>
<td>an array with the same data as the given array, but with
different dimensions.</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.copy" title="Base.copy"><tt class="xref jl jl-func docutils literal"><span class="pre">copy(A)</span></tt></a></td>
<td>copy <tt class="docutils literal"><span class="pre">A</span></tt></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.deepcopy" title="Base.deepcopy"><tt class="xref jl jl-func docutils literal"><span class="pre">deepcopy(A)</span></tt></a></td>
<td>copy <tt class="docutils literal"><span class="pre">A</span></tt>, recursively copying its elements</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.similar" title="Base.similar"><tt class="xref jl jl-func docutils literal"><span class="pre">similar(A,</span> <span class="pre">element_type,</span> <span class="pre">dims...)</span></tt></a></td>
<td>an uninitialized array of the same type as the given array
(dense, sparse, etc.), but with the specified element type and
dimensions. The second and third arguments are both optional,
defaulting to the element type and dimensions of <tt class="docutils literal"><span class="pre">A</span></tt> if omitted.</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.reinterpret" title="Base.reinterpret"><tt class="xref jl jl-func docutils literal"><span class="pre">reinterpret(type,</span> <span class="pre">A)</span></tt></a></td>
<td>an array with the same binary data as the given array, but with the
specified element type</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.rand" title="Base.rand"><tt class="xref jl jl-func docutils literal"><span class="pre">rand(dims)</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">Array</span></tt> of <tt class="docutils literal"><span class="pre">Float64</span></tt>s with random, iid[#]_ and uniformly
distributed values in [0,1)</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.randn" title="Base.randn"><tt class="xref jl jl-func docutils literal"><span class="pre">randn(dims)</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">Array</span></tt> of <tt class="docutils literal"><span class="pre">Float64</span></tt>s with random, iid and standard normally
distributed random values</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.eye" title="Base.eye"><tt class="xref jl jl-func docutils literal"><span class="pre">eye(n)</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">n</span></tt>-by-<tt class="docutils literal"><span class="pre">n</span></tt> identity matrix</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.eye" title="Base.eye"><tt class="xref jl jl-func docutils literal"><span class="pre">eye(m,</span> <span class="pre">n)</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">m</span></tt>-by-<tt class="docutils literal"><span class="pre">n</span></tt> identity matrix</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.linspace" title="Base.linspace"><tt class="xref jl jl-func docutils literal"><span class="pre">linspace(start,</span> <span class="pre">stop,</span> <span class="pre">n)</span></tt></a></td>
<td>vector of <tt class="docutils literal"><span class="pre">n</span></tt> linearly-spaced elements from <tt class="docutils literal"><span class="pre">start</span></tt> to <tt class="docutils literal"><span class="pre">stop</span></tt></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.fill!" title="Base.fill!"><tt class="xref jl jl-func docutils literal"><span class="pre">fill!(A,</span> <span class="pre">x)</span></tt></a></td>
<td>fill the array <tt class="docutils literal"><span class="pre">A</span></tt> with the value <tt class="docutils literal"><span class="pre">x</span></tt></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.fill" title="Base.fill"><tt class="xref jl jl-func docutils literal"><span class="pre">fill(x,</span> <span class="pre">dims)</span></tt></a></td>
<td>create an array filled with the value <tt class="docutils literal"><span class="pre">x</span></tt></td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[1]</td><td><em>iid</em>, independently and identically distributed.</td></tr>
</tbody>
</table>
</div>
<div class="section" id="concatenation">
<h3>Concatenation<a class="headerlink" href="#concatenation" title="Permalink to this headline">¶</a></h3>
<p>Arrays can be constructed and also concatenated using the following
functions:</p>
<table border="1" class="docutils">
<colgroup>
<col width="33%" />
<col width="67%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Function</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.cat" title="Base.cat"><tt class="xref jl jl-func docutils literal"><span class="pre">cat(k,</span> <span class="pre">A...)</span></tt></a></td>
<td>concatenate input n-d arrays along the dimension <tt class="docutils literal"><span class="pre">k</span></tt></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/base.html#Base.vcat" title="Base.vcat"><tt class="xref jl jl-func docutils literal"><span class="pre">vcat(A...)</span></tt></a></td>
<td>shorthand for <tt class="docutils literal"><span class="pre">cat(1,</span> <span class="pre">A...)</span></tt></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/base.html#Base.hcat" title="Base.hcat"><tt class="xref jl jl-func docutils literal"><span class="pre">hcat(A...)</span></tt></a></td>
<td>shorthand for <tt class="docutils literal"><span class="pre">cat(2,</span> <span class="pre">A...)</span></tt></td>
</tr>
</tbody>
</table>
<p>Scalar values passed to these functions are treated as 1-element arrays.</p>
<p>The concatenation functions are used so often that they have special syntax:</p>
<table border="1" class="docutils">
<colgroup>
<col width="59%" />
<col width="41%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Expression</th>
<th class="head">Calls</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">[A</span> <span class="pre">B</span> <span class="pre">C</span> <span class="pre">...]</span></tt></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.hcat" title="Base.hcat"><tt class="xref jl jl-func docutils literal"><span class="pre">hcat()</span></tt></a></td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">[A,</span> <span class="pre">B,</span> <span class="pre">C,</span> <span class="pre">...]</span></tt></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.vcat" title="Base.vcat"><tt class="xref jl jl-func docutils literal"><span class="pre">vcat()</span></tt></a></td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">[A</span> <span class="pre">B;</span> <span class="pre">C</span> <span class="pre">D;</span> <span class="pre">...]</span></tt></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.hvcat" title="Base.hvcat"><tt class="xref jl jl-func docutils literal"><span class="pre">hvcat()</span></tt></a></td>
</tr>
</tbody>
</table>
<p><a class="reference internal" href="../stdlib/base.html#Base.hvcat" title="Base.hvcat"><tt class="xref jl jl-func docutils literal"><span class="pre">hvcat()</span></tt></a> concatenates in both dimension 1 (with semicolons) and dimension 2
(with spaces).</p>
</div>
<div class="section" id="typed-array-initializers">
<h3>Typed array initializers<a class="headerlink" href="#typed-array-initializers" title="Permalink to this headline">¶</a></h3>
<p>An array with a specific element type can be constructed using the syntax
<tt class="docutils literal"><span class="pre">T[A,</span> <span class="pre">B,</span> <span class="pre">C,</span> <span class="pre">...]</span></tt>. This will construct a 1-d array with element type
<tt class="docutils literal"><span class="pre">T</span></tt>, initialized to contain elements <tt class="docutils literal"><span class="pre">A</span></tt>, <tt class="docutils literal"><span class="pre">B</span></tt>, <tt class="docutils literal"><span class="pre">C</span></tt>, etc.</p>
<p>Special syntax is available for constructing arrays with element type
<tt class="docutils literal"><span class="pre">Any</span></tt>:</p>
<table border="1" class="docutils">
<colgroup>
<col width="40%" />
<col width="60%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Expression</th>
<th class="head">Yields</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">{A</span> <span class="pre">B</span> <span class="pre">C</span> <span class="pre">...}</span></tt></td>
<td>A 1xN <tt class="docutils literal"><span class="pre">Any</span></tt> array</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">{A,</span> <span class="pre">B,</span> <span class="pre">C,</span> <span class="pre">...}</span></tt></td>
<td>A 1-d <tt class="docutils literal"><span class="pre">Any</span></tt> array (vector)</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">{A</span> <span class="pre">B;</span> <span class="pre">C</span> <span class="pre">D;</span> <span class="pre">...}</span></tt></td>
<td>A 2-d <tt class="docutils literal"><span class="pre">Any</span></tt> array</td>
</tr>
</tbody>
</table>
<p>Note that this form does not do any concatenation; each argument becomes
an element of the resulting array.</p>
</div>
<div class="section" id="comprehensions">
<h3>Comprehensions<a class="headerlink" href="#comprehensions" title="Permalink to this headline">¶</a></h3>
<p>Comprehensions provide a general and powerful way to construct arrays.
Comprehension syntax is similar to set construction notation in
mathematics:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">A</span> <span class="o">=</span> <span class="p">[</span> <span class="n">F</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="o">...</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span><span class="o">=</span><span class="n">rx</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">ry</span><span class="p">,</span> <span class="o">...</span> <span class="p">]</span>
</pre></div>
</div>
<p>The meaning of this form is that <tt class="docutils literal"><span class="pre">F(x,y,...)</span></tt> is evaluated with the
variables <tt class="docutils literal"><span class="pre">x</span></tt>, <tt class="docutils literal"><span class="pre">y</span></tt>, etc. taking on each value in their given list of
values. Values can be specified as any iterable object, but will
commonly be ranges like <tt class="docutils literal"><span class="pre">1:n</span></tt> or <tt class="docutils literal"><span class="pre">2:(n-1)</span></tt>, or explicit arrays of
values like <tt class="docutils literal"><span class="pre">[1.2,</span> <span class="pre">3.4,</span> <span class="pre">5.7]</span></tt>. The result is an N-d dense array with
dimensions that are the concatenation of the dimensions of the variable
ranges <tt class="docutils literal"><span class="pre">rx</span></tt>, <tt class="docutils literal"><span class="pre">ry</span></tt>, etc. and each <tt class="docutils literal"><span class="pre">F(x,y,...)</span></tt> evaluation returns a
scalar.</p>
<p>The following example computes a weighted average of the current element
and its left and right neighbor along a 1-d grid. :</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="kd">const</span> <span class="n">x</span> <span class="o">=</span> <span class="n">rand</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span>
<span class="mi">8</span><span class="o">-</span><span class="n">element</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Float64</span><span class="p">,</span><span class="mi">1</span><span class="p">}:</span>
 <span class="mf">0.843025</span>
 <span class="mf">0.869052</span>
 <span class="mf">0.365105</span>
 <span class="mf">0.699456</span>
 <span class="mf">0.977653</span>
 <span class="mf">0.994953</span>
 <span class="mf">0.41084</span>
 <span class="mf">0.809411</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="p">[</span> <span class="mf">0.25</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="mf">0.25</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span> <span class="p">]</span>
<span class="mi">6</span><span class="o">-</span><span class="n">element</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Float64</span><span class="p">,</span><span class="mi">1</span><span class="p">}:</span>
 <span class="mf">0.736559</span>
 <span class="mf">0.57468</span>
 <span class="mf">0.685417</span>
 <span class="mf">0.912429</span>
 <span class="mf">0.8446</span>
 <span class="mf">0.656511</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In the above example, <tt class="docutils literal"><span class="pre">x</span></tt> is declared as constant because type
inference in Julia does not work as well on non-constant global
variables.</p>
</div>
<p>The resulting array type is inferred from the expression; in order to control
the type explicitly, the type can be prepended to the comprehension. For example,
in the above example we could have avoided declaring <tt class="docutils literal"><span class="pre">x</span></tt> as constant, and ensured
that the result is of type <tt class="docutils literal"><span class="pre">Float64</span></tt> by writing:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="kt">Float64</span><span class="p">[</span> <span class="mf">0.25</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="mf">0.25</span><span class="o">*</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span> <span class="p">]</span>
</pre></div>
</div>
<p>Using curly brackets instead of square brackets is a shorthand notation for an
array of type <tt class="docutils literal"><span class="pre">Any</span></tt>:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="p">{</span> <span class="n">i</span><span class="o">/</span><span class="mi">2</span> <span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">:</span><span class="mi">3</span> <span class="p">}</span>
<span class="mi">3</span><span class="o">-</span><span class="n">element</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Any</span><span class="p">,</span><span class="mi">1</span><span class="p">}:</span>
 <span class="mf">0.5</span>
 <span class="mf">1.0</span>
 <span class="mf">1.5</span>
</pre></div>
</div>
</div>
<div class="section" id="indexing">
<span id="man-array-indexing"></span><h3>Indexing<a class="headerlink" href="#indexing" title="Permalink to this headline">¶</a></h3>
<p>The general syntax for indexing into an n-dimensional array A is:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">X</span> <span class="o">=</span> <span class="n">A</span><span class="p">[</span><span class="n">I_1</span><span class="p">,</span> <span class="n">I_2</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">I_n</span><span class="p">]</span>
</pre></div>
</div>
<p>where each I_k may be:</p>
<ol class="arabic simple">
<li>A scalar value</li>
<li>A <tt class="docutils literal"><span class="pre">Range</span></tt> of the form <tt class="docutils literal"><span class="pre">:</span></tt>, <tt class="docutils literal"><span class="pre">a:b</span></tt>, or <tt class="docutils literal"><span class="pre">a:b:c</span></tt></li>
<li>An arbitrary integer vector, including the empty vector <tt class="docutils literal"><span class="pre">[]</span></tt></li>
<li>A boolean vector</li>
</ol>
<p>The result X generally has dimensions
<tt class="docutils literal"><span class="pre">(length(I_1),</span> <span class="pre">length(I_2),</span> <span class="pre">...,</span> <span class="pre">length(I_n))</span></tt>, with location
<tt class="docutils literal"><span class="pre">(i_1,</span> <span class="pre">i_2,</span> <span class="pre">...,</span> <span class="pre">i_n)</span></tt> of X containing the value
<tt class="docutils literal"><span class="pre">A[I_1[i_1],</span> <span class="pre">I_2[i_2],</span> <span class="pre">...,</span> <span class="pre">I_n[i_n]]</span></tt>. Trailing dimensions indexed with
scalars are dropped. For example, the dimensions of <tt class="docutils literal"><span class="pre">A[I,</span> <span class="pre">1]</span></tt> will be
<tt class="docutils literal"><span class="pre">(length(I),)</span></tt>. Boolean vectors are first transformed with <tt class="docutils literal"><span class="pre">find</span></tt>; the size of
a dimension indexed by a boolean vector will be the number of true values in the vector.</p>
<p>Indexing syntax is equivalent to a call to <tt class="docutils literal"><span class="pre">getindex</span></tt>:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">X</span> <span class="o">=</span> <span class="n">getindex</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">I_1</span><span class="p">,</span> <span class="n">I_2</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">I_n</span><span class="p">)</span>
</pre></div>
</div>
<p>Example:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="mi">16</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="mi">4</span><span class="n">x4</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Int64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mi">1</span>  <span class="mi">5</span>   <span class="mi">9</span>  <span class="mi">13</span>
 <span class="mi">2</span>  <span class="mi">6</span>  <span class="mi">10</span>  <span class="mi">14</span>
 <span class="mi">3</span>  <span class="mi">7</span>  <span class="mi">11</span>  <span class="mi">15</span>
 <span class="mi">4</span>  <span class="mi">8</span>  <span class="mi">12</span>  <span class="mi">16</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span><span class="k">end</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="mi">2</span><span class="n">x2</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Int64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mi">6</span>  <span class="mi">10</span>
 <span class="mi">7</span>  <span class="mi">11</span>
</pre></div>
</div>
<p>Empty ranges of the form <tt class="docutils literal"><span class="pre">n:n-1</span></tt> are sometimes used to indicate the inter-index
location between <tt class="docutils literal"><span class="pre">n-1</span></tt> and <tt class="docutils literal"><span class="pre">n</span></tt>.  For example, the <a class="reference internal" href="../stdlib/sort.html#Base.searchsorted" title="Base.searchsorted"><tt class="xref jl jl-func docutils literal"><span class="pre">searchsorted()</span></tt></a> function uses
this convention to indicate the insertion point of a value not found in a sorted
array:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">];</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">searchsorted</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="mi">3</span><span class="p">:</span><span class="mi">2</span>
</pre></div>
</div>
</div>
<div class="section" id="assignment">
<h3>Assignment<a class="headerlink" href="#assignment" title="Permalink to this headline">¶</a></h3>
<p>The general syntax for assigning values in an n-dimensional array A is:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">A</span><span class="p">[</span><span class="n">I_1</span><span class="p">,</span> <span class="n">I_2</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">I_n</span><span class="p">]</span> <span class="o">=</span> <span class="n">X</span>
</pre></div>
</div>
<p>where each I_k may be:</p>
<ol class="arabic simple">
<li>A scalar value</li>
<li>A <tt class="docutils literal"><span class="pre">Range</span></tt> of the form <tt class="docutils literal"><span class="pre">:</span></tt>, <tt class="docutils literal"><span class="pre">a:b</span></tt>, or <tt class="docutils literal"><span class="pre">a:b:c</span></tt></li>
<li>An arbitrary integer vector, including the empty vector <tt class="docutils literal"><span class="pre">[]</span></tt></li>
<li>A boolean vector</li>
</ol>
<p>If <tt class="docutils literal"><span class="pre">X</span></tt> is an array, its size must be <tt class="docutils literal"><span class="pre">(length(I_1),</span> <span class="pre">length(I_2),</span> <span class="pre">...,</span> <span class="pre">length(I_n))</span></tt>,
and the value in location <tt class="docutils literal"><span class="pre">i_1,</span> <span class="pre">i_2,</span> <span class="pre">...,</span> <span class="pre">i_n</span></tt> of <tt class="docutils literal"><span class="pre">A</span></tt> is overwritten with
the value <tt class="docutils literal"><span class="pre">X[I_1[i_1],</span> <span class="pre">I_2[i_2],</span> <span class="pre">...,</span> <span class="pre">I_n[i_n]]</span></tt>. If <tt class="docutils literal"><span class="pre">X</span></tt> is not an array, its
value is written to all referenced locations of <tt class="docutils literal"><span class="pre">A</span></tt>.</p>
<p>A boolean vector used as an index behaves as in <a class="reference internal" href="../stdlib/base.html#Base.getindex" title="Base.getindex"><tt class="xref jl jl-func docutils literal"><span class="pre">getindex()</span></tt></a> (it is first transformed
with <a class="reference internal" href="../stdlib/base.html#Base.find" title="Base.find"><tt class="xref jl jl-func docutils literal"><span class="pre">find()</span></tt></a>).</p>
<p>Index assignment syntax is equivalent to a call to <a class="reference internal" href="../stdlib/base.html#Base.setindex!" title="Base.setindex!"><tt class="xref jl jl-func docutils literal"><span class="pre">setindex!()</span></tt></a>:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">setindex</span><span class="o">!</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">I_1</span><span class="p">,</span> <span class="n">I_2</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">I_n</span><span class="p">)</span>
</pre></div>
</div>
<p>Example:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="mi">9</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="mi">3</span><span class="n">x3</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Int64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mi">1</span>  <span class="mi">4</span>  <span class="mi">7</span>
 <span class="mi">2</span>  <span class="mi">5</span>  <span class="mi">8</span>
 <span class="mi">3</span>  <span class="mi">6</span>  <span class="mi">9</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
<span class="o">-</span><span class="mi">1</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">x</span>
<span class="mi">3</span><span class="n">x3</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Int64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mi">1</span>  <span class="o">-</span><span class="mi">1</span>  <span class="o">-</span><span class="mi">1</span>
 <span class="mi">2</span>  <span class="o">-</span><span class="mi">1</span>  <span class="o">-</span><span class="mi">1</span>
 <span class="mi">3</span>   <span class="mi">6</span>   <span class="mi">9</span>
</pre></div>
</div>
</div>
<div class="section" id="vectorized-operators-and-functions">
<h3>Vectorized Operators and Functions<a class="headerlink" href="#vectorized-operators-and-functions" title="Permalink to this headline">¶</a></h3>
<p>The following operators are supported for arrays. The dot version of a binary
operator should be used for elementwise operations.</p>
<ol class="arabic simple">
<li>Unary arithmetic — <tt class="docutils literal"><span class="pre">-</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">!</span></tt></li>
<li>Binary arithmetic — <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt>, <tt class="docutils literal"><span class="pre">*</span></tt>, <tt class="docutils literal"><span class="pre">.*</span></tt>, <tt class="docutils literal"><span class="pre">/</span></tt>, <tt class="docutils literal"><span class="pre">./</span></tt>,
<tt class="docutils literal"><span class="pre">\</span></tt>, <tt class="docutils literal"><span class="pre">.\</span></tt>, <tt class="docutils literal"><span class="pre">^</span></tt>, <tt class="docutils literal"><span class="pre">.^</span></tt>, <tt class="docutils literal"><span class="pre">div</span></tt>, <tt class="docutils literal"><span class="pre">mod</span></tt></li>
<li>Comparison — <tt class="docutils literal"><span class="pre">.==</span></tt>, <tt class="docutils literal"><span class="pre">.!=</span></tt>, <tt class="docutils literal"><span class="pre">.&lt;</span></tt>, <tt class="docutils literal"><span class="pre">.&lt;=</span></tt>, <tt class="docutils literal"><span class="pre">.&gt;</span></tt>, <tt class="docutils literal"><span class="pre">.&gt;=</span></tt></li>
<li>Unary Boolean or bitwise — <tt class="docutils literal"><span class="pre">~</span></tt></li>
<li>Binary Boolean or bitwise — <tt class="docutils literal"><span class="pre">&amp;</span></tt>, <tt class="docutils literal"><span class="pre">|</span></tt>, <tt class="docutils literal"><span class="pre">$</span></tt></li>
</ol>
<p>Some operators without dots operate elementwise anyway when one argument is a
scalar. These operators are <tt class="docutils literal"><span class="pre">*</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt>, and the bitwise operators. The
operators <tt class="docutils literal"><span class="pre">/</span></tt> and <tt class="docutils literal"><span class="pre">\</span></tt> operate elementwise when the denominator is a scalar.</p>
<p>Note that comparisons such as <tt class="docutils literal"><span class="pre">==</span></tt> operate on whole arrays, giving a single
boolean answer. Use dot operators for elementwise comparisons.</p>
<p>The following built-in functions are also vectorized, whereby the functions act
elementwise:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">abs</span> <span class="n">abs2</span> <span class="n">angle</span> <span class="n">cbrt</span>
<span class="n">airy</span> <span class="n">airyai</span> <span class="n">airyaiprime</span> <span class="n">airybi</span> <span class="n">airybiprime</span> <span class="n">airyprime</span>
<span class="n">acos</span> <span class="n">acosh</span> <span class="n">asin</span> <span class="n">asinh</span> <span class="n">atan</span> <span class="n">atan2</span> <span class="n">atanh</span>
<span class="n">acsc</span> <span class="n">acsch</span> <span class="n">asec</span> <span class="n">asech</span> <span class="n">acot</span> <span class="n">acoth</span>
<span class="n">cos</span>  <span class="n">cospi</span> <span class="n">cosh</span>  <span class="n">sin</span>  <span class="n">sinpi</span> <span class="n">sinh</span>  <span class="n">tan</span>  <span class="n">tanh</span>  <span class="n">sinc</span>  <span class="n">cosc</span>
<span class="n">csc</span>  <span class="n">csch</span>  <span class="n">sec</span>  <span class="n">sech</span>  <span class="n">cot</span>  <span class="n">coth</span>
<span class="n">acosd</span> <span class="n">asind</span> <span class="n">atand</span> <span class="n">asecd</span> <span class="n">acscd</span> <span class="n">acotd</span>
<span class="n">cosd</span>  <span class="n">sind</span>  <span class="n">tand</span>  <span class="n">secd</span>  <span class="n">cscd</span>  <span class="n">cotd</span>
<span class="n">besselh</span> <span class="n">besseli</span> <span class="n">besselj</span> <span class="n">besselj0</span> <span class="n">besselj1</span> <span class="n">besselk</span> <span class="n">bessely</span> <span class="n">bessely0</span> <span class="n">bessely1</span>
<span class="n">exp</span>  <span class="n">erf</span>  <span class="n">erfc</span>  <span class="n">erfinv</span> <span class="n">erfcinv</span> <span class="n">exp2</span>  <span class="n">expm1</span>
<span class="n">beta</span> <span class="n">dawson</span> <span class="n">digamma</span> <span class="n">erfcx</span> <span class="n">erfi</span>
<span class="n">exponent</span> <span class="n">eta</span> <span class="n">zeta</span> <span class="n">gamma</span>
<span class="n">hankelh1</span> <span class="n">hankelh2</span>
 <span class="n">ceil</span>  <span class="n">floor</span>  <span class="n">round</span>  <span class="n">trunc</span>
<span class="n">iceil</span> <span class="n">ifloor</span> <span class="n">iround</span> <span class="n">itrunc</span>
<span class="n">isfinite</span> <span class="n">isinf</span> <span class="n">isnan</span>
<span class="n">lbeta</span> <span class="n">lfact</span> <span class="n">lgamma</span>
<span class="n">log</span> <span class="n">log10</span> <span class="n">log1p</span> <span class="n">log2</span>
<span class="n">copysign</span> <span class="n">max</span> <span class="n">min</span> <span class="n">significand</span>
<span class="n">sqrt</span> <span class="n">hypot</span>
</pre></div>
</div>
<p>Note that there is a difference between <a class="reference internal" href="../stdlib/base.html#Base.min" title="Base.min"><tt class="xref jl jl-func docutils literal"><span class="pre">min()</span></tt></a> and <a class="reference internal" href="../stdlib/base.html#Base.max" title="Base.max"><tt class="xref jl jl-func docutils literal"><span class="pre">max()</span></tt></a>, which operate
elementwise over multiple array arguments, and <a class="reference internal" href="../stdlib/base.html#Base.minimum" title="Base.minimum"><tt class="xref jl jl-func docutils literal"><span class="pre">minimum()</span></tt></a> and <a class="reference internal" href="../stdlib/base.html#Base.maximum" title="Base.maximum"><tt class="xref jl jl-func docutils literal"><span class="pre">maximum()</span></tt></a>, which
find the smallest and largest values within an array.</p>
<p>Julia provides the <tt class="xref jl jl-func docutils literal"><span class="pre">&#64;vectorize_1arg()</span></tt> and <tt class="xref jl jl-func docutils literal"><span class="pre">&#64;vectorize_2arg()</span></tt>
macros to automatically vectorize any function of one or two arguments
respectively.  Each of these takes two arguments, namely the <tt class="docutils literal"><span class="pre">Type</span></tt> of
argument (which is usually chosen to be the most general possible) and
the name of the function to vectorize. Here is a simple example:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">square</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">=</span> <span class="n">x</span><span class="o">^</span><span class="mi">2</span>
<span class="n">square</span> <span class="p">(</span><span class="n">generic</span> <span class="k">function</span><span class="nf"> with</span> <span class="mi">1</span> <span class="n">method</span><span class="p">)</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="p">@</span><span class="n">vectorize_1arg</span> <span class="n">Number</span> <span class="n">square</span>
<span class="n">square</span> <span class="p">(</span><span class="n">generic</span> <span class="k">function</span><span class="nf"> with</span> <span class="mi">4</span> <span class="n">methods</span><span class="p">)</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">methods</span><span class="p">(</span><span class="n">square</span><span class="p">)</span>
<span class="c"># 4 methods for generic function &quot;square&quot;:</span>
<span class="n">square</span><span class="p">{</span><span class="n">T</span><span class="o">&lt;:</span><span class="n">Number</span><span class="p">}(::</span><span class="n">AbstractArray</span><span class="p">{</span><span class="n">T</span><span class="o">&lt;:</span><span class="n">Number</span><span class="p">,</span><span class="mi">1</span><span class="p">})</span> <span class="n">at</span> <span class="n">operators</span><span class="o">.</span><span class="n">jl</span><span class="p">:</span><span class="mi">359</span>
<span class="n">square</span><span class="p">{</span><span class="n">T</span><span class="o">&lt;:</span><span class="n">Number</span><span class="p">}(::</span><span class="n">AbstractArray</span><span class="p">{</span><span class="n">T</span><span class="o">&lt;:</span><span class="n">Number</span><span class="p">,</span><span class="mi">2</span><span class="p">})</span> <span class="n">at</span> <span class="n">operators</span><span class="o">.</span><span class="n">jl</span><span class="p">:</span><span class="mi">360</span>
<span class="n">square</span><span class="p">{</span><span class="n">T</span><span class="o">&lt;:</span><span class="n">Number</span><span class="p">}(::</span><span class="n">AbstractArray</span><span class="p">{</span><span class="n">T</span><span class="o">&lt;:</span><span class="n">Number</span><span class="p">,</span><span class="n">N</span><span class="p">})</span> <span class="n">at</span> <span class="n">operators</span><span class="o">.</span><span class="n">jl</span><span class="p">:</span><span class="mi">362</span>
<span class="n">square</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="n">at</span> <span class="n">none</span><span class="p">:</span><span class="mi">1</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">square</span><span class="p">([</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">4</span><span class="p">;</span> <span class="mi">5</span> <span class="mi">6</span> <span class="mi">7</span><span class="p">])</span>
<span class="mi">2</span><span class="n">x3</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Int64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
  <span class="mi">1</span>   <span class="mi">4</span>  <span class="mi">16</span>
 <span class="mi">25</span>  <span class="mi">36</span>  <span class="mi">49</span>
</pre></div>
</div>
</div>
<div class="section" id="broadcasting">
<h3>Broadcasting<a class="headerlink" href="#broadcasting" title="Permalink to this headline">¶</a></h3>
<p>It is sometimes useful to perform element-by-element binary operations
on arrays of different sizes, such as adding a vector to each column
of a matrix.  An inefficient way to do this would be to replicate the
vector to the size of the matrix:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span> <span class="n">A</span> <span class="o">=</span> <span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">repmat</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="n">A</span>
<span class="mi">2</span><span class="n">x3</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Float64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mf">1.20813</span>  <span class="mf">1.82068</span>  <span class="mf">1.25387</span>
 <span class="mf">1.56851</span>  <span class="mf">1.86401</span>  <span class="mf">1.67846</span>
</pre></div>
</div>
<p>This is wasteful when dimensions get large, so Julia offers
<a class="reference internal" href="../stdlib/base.html#Base.broadcast" title="Base.broadcast"><tt class="xref jl jl-func docutils literal"><span class="pre">broadcast()</span></tt></a>, which expands singleton dimensions in
array arguments to match the corresponding dimension in the other
array without using extra memory, and applies the given
function elementwise:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">broadcast</span><span class="p">(</span><span class="o">+</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">A</span><span class="p">)</span>
<span class="mi">2</span><span class="n">x3</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Float64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mf">1.20813</span>  <span class="mf">1.82068</span>  <span class="mf">1.25387</span>
 <span class="mf">1.56851</span>  <span class="mf">1.86401</span>  <span class="mf">1.67846</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">b</span> <span class="o">=</span> <span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">1</span><span class="n">x2</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Float64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mf">0.867535</span>  <span class="mf">0.00457906</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">broadcast</span><span class="p">(</span><span class="o">+</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="mi">2</span><span class="n">x2</span> <span class="n">Array</span><span class="p">{</span><span class="kt">Float64</span><span class="p">,</span><span class="mi">2</span><span class="p">}:</span>
 <span class="mf">1.71056</span>  <span class="mf">0.847604</span>
 <span class="mf">1.73659</span>  <span class="mf">0.873631</span>
</pre></div>
</div>
<p>Elementwise operators such as <tt class="docutils literal"><span class="pre">.+</span></tt> and <tt class="docutils literal"><span class="pre">.*</span></tt> perform broadcasting if necessary. There is also a <a class="reference internal" href="../stdlib/base.html#Base.broadcast!" title="Base.broadcast!"><tt class="xref jl jl-func docutils literal"><span class="pre">broadcast!()</span></tt></a> function to specify an explicit destination, and <a class="reference internal" href="../stdlib/base.html#Base.broadcast_getindex" title="Base.broadcast_getindex"><tt class="xref jl jl-func docutils literal"><span class="pre">broadcast_getindex()</span></tt></a> and <a class="reference internal" href="../stdlib/base.html#Base.broadcast_setindex!" title="Base.broadcast_setindex!"><tt class="xref jl jl-func docutils literal"><span class="pre">broadcast_setindex!()</span></tt></a> that broadcast the indices before indexing.</p>
</div>
<div class="section" id="implementation">
<h3>Implementation<a class="headerlink" href="#implementation" title="Permalink to this headline">¶</a></h3>
<p>The base array type in Julia is the abstract type
<tt class="docutils literal"><span class="pre">AbstractArray{T,N}</span></tt>. It is parametrized by the number of dimensions
<tt class="docutils literal"><span class="pre">N</span></tt> and the element type <tt class="docutils literal"><span class="pre">T</span></tt>. <tt class="docutils literal"><span class="pre">AbstractVector</span></tt> and
<tt class="docutils literal"><span class="pre">AbstractMatrix</span></tt> are aliases for the 1-d and 2-d cases. Operations on
<tt class="docutils literal"><span class="pre">AbstractArray</span></tt> objects are defined using higher level operators and
functions, in a way that is independent of the underlying storage.
These operations generally work correctly as a fallback for any
specific array implementation.</p>
<p>The <tt class="docutils literal"><span class="pre">AbstractArray</span></tt> type includes anything vaguely array-like, and
implementations of it might be quite different from conventional
arrays. For example, elements might be computed on request rather than
stored.  However, any concrete <tt class="docutils literal"><span class="pre">AbstractArray{T,N}</span></tt> type should
generally implement at least <a class="reference internal" href="../stdlib/base.html#Base.size" title="Base.size"><tt class="xref jl jl-func docutils literal"><span class="pre">size(A)</span></tt></a> (returing an <tt class="docutils literal"><span class="pre">Int</span></tt> tuple),
<a class="reference internal" href="../stdlib/base.html#Base.getindex" title="Base.getindex"><tt class="xref jl jl-func docutils literal"><span class="pre">getindex(A,i)</span></tt></a> and <a class="reference internal" href="../stdlib/base.html#Base.getindex" title="Base.getindex"><tt class="xref jl jl-func docutils literal"><span class="pre">getindex(A,i1,...,iN)</span></tt></a> (returning an element
of type <tt class="docutils literal"><span class="pre">T</span></tt>); mutable arrays should also implement <a class="reference internal" href="../stdlib/base.html#Base.setindex!" title="Base.setindex!"><tt class="xref jl jl-func docutils literal"><span class="pre">setindex!()</span></tt></a>.  It
is recommended that these operations have nearly constant time complexity,
or technically Õ(1) complexity, as otherwise some array functions may
be unexpectedly slow.   Concrete types should also typically provide
a <a class="reference internal" href="../stdlib/base.html#Base.similar" title="Base.similar"><tt class="xref jl jl-func docutils literal"><span class="pre">similar(A,T=eltype(A),dims=size(A))</span></tt></a> method, which is used to allocate
a similar array for <a class="reference internal" href="../stdlib/base.html#Base.copy" title="Base.copy"><tt class="xref jl jl-func docutils literal"><span class="pre">copy()</span></tt></a> and other out-of-place operations.</p>
<p><tt class="docutils literal"><span class="pre">DenseArray</span></tt> is an abstract subtype of <tt class="docutils literal"><span class="pre">AbstractArray</span></tt> intended
to include all arrays that are laid out at regular offsets in memory,
and which can therefore be passed to external C and Fortran functions
expecting this memory layout.  Subtypes should provide a method
<a class="reference internal" href="../stdlib/base.html#Base.stride" title="Base.stride"><tt class="xref jl jl-func docutils literal"><span class="pre">stride(A,k)</span></tt></a> that returns the &#8220;stride&#8221; of dimension <tt class="docutils literal"><span class="pre">k</span></tt>:
increasing the index of dimension <tt class="docutils literal"><span class="pre">k</span></tt> by <tt class="docutils literal"><span class="pre">1</span></tt> should increase the
index <tt class="docutils literal"><span class="pre">i</span></tt> of <a class="reference internal" href="../stdlib/base.html#Base.getindex" title="Base.getindex"><tt class="xref jl jl-func docutils literal"><span class="pre">getindex(A,i)</span></tt></a> by <a class="reference internal" href="../stdlib/base.html#Base.stride" title="Base.stride"><tt class="xref jl jl-func docutils literal"><span class="pre">stride(A,k)</span></tt></a>.  If a
pointer conversion method <a class="reference internal" href="../stdlib/base.html#Base.convert" title="Base.convert"><tt class="xref jl jl-func docutils literal"><span class="pre">convert(Ptr{T},</span> <span class="pre">A)</span></tt></a> is provided, the
memory layout should correspond in the same way to these strides.</p>
<p>The <tt class="docutils literal"><span class="pre">Array{T,N}</span></tt> type is a specific instance of <tt class="docutils literal"><span class="pre">DenseArray</span></tt>
where elements are stored in column-major order (see additional notes in
<a class="reference internal" href="performance-tips.html#man-performance-tips"><em>Performance Tips</em></a>). <tt class="docutils literal"><span class="pre">Vector</span></tt> and <tt class="docutils literal"><span class="pre">Matrix</span></tt> are aliases for
the 1-d and 2-d cases. Specific operations such as scalar indexing,
assignment, and a few other basic storage-specific operations are all
that have to be implemented for <tt class="docutils literal"><span class="pre">Array</span></tt>, so that the rest of the array
library can be implemented in a generic manner.</p>
<p><tt class="docutils literal"><span class="pre">SubArray</span></tt> is a specialization of <tt class="docutils literal"><span class="pre">AbstractArray</span></tt> that performs
indexing by reference rather than by copying. A <tt class="docutils literal"><span class="pre">SubArray</span></tt> is created
with the <a class="reference internal" href="../stdlib/base.html#Base.sub" title="Base.sub"><tt class="xref jl jl-func docutils literal"><span class="pre">sub()</span></tt></a> function, which is called the same way as <a class="reference internal" href="../stdlib/base.html#Base.getindex" title="Base.getindex"><tt class="xref jl jl-func docutils literal"><span class="pre">getindex()</span></tt></a> (with
an array and a series of index arguments). The result of <a class="reference internal" href="../stdlib/base.html#Base.sub" title="Base.sub"><tt class="xref jl jl-func docutils literal"><span class="pre">sub()</span></tt></a> looks
the same as the result of <a class="reference internal" href="../stdlib/base.html#Base.getindex" title="Base.getindex"><tt class="xref jl jl-func docutils literal"><span class="pre">getindex()</span></tt></a>, except the data is left in place.
<a class="reference internal" href="../stdlib/base.html#Base.sub" title="Base.sub"><tt class="xref jl jl-func docutils literal"><span class="pre">sub()</span></tt></a> stores the input index vectors in a <tt class="docutils literal"><span class="pre">SubArray</span></tt> object, which
can later be used to index the original array indirectly.</p>
<p><tt class="docutils literal"><span class="pre">StridedVector</span></tt> and <tt class="docutils literal"><span class="pre">StridedMatrix</span></tt> are convenient aliases defined
to make it possible for Julia to call a wider range of BLAS and LAPACK
functions by passing them either <tt class="docutils literal"><span class="pre">Array</span></tt> or <tt class="docutils literal"><span class="pre">SubArray</span></tt> objects, and
thus saving inefficiencies from memory allocation and copying.</p>
<p>The following example computes the QR decomposition of a small section
of a larger array, without creating any temporaries, and by calling the
appropriate LAPACK function with the right leading dimension size and
stride parameters.</p>
<div class="highlight-julia"><pre>julia&gt; a = rand(10,10)
10x10 Array{Float64,2}:
 0.561255   0.226678   0.203391  0.308912   …  0.750307  0.235023   0.217964
 0.718915   0.537192   0.556946  0.996234      0.666232  0.509423   0.660788
 0.493501   0.0565622  0.118392  0.493498      0.262048  0.940693   0.252965
 0.0470779  0.736979   0.264822  0.228787      0.161441  0.897023   0.567641
 0.343935   0.32327    0.795673  0.452242      0.468819  0.628507   0.511528
 0.935597   0.991511   0.571297  0.74485    …  0.84589   0.178834   0.284413
 0.160706   0.672252   0.133158  0.65554       0.371826  0.770628   0.0531208
 0.306617   0.836126   0.301198  0.0224702     0.39344   0.0370205  0.536062
 0.890947   0.168877   0.32002   0.486136      0.096078  0.172048   0.77672
 0.507762   0.573567   0.220124  0.165816      0.211049  0.433277   0.539476

julia&gt; b = sub(a, 2:2:8,2:2:4)
4x2 SubArray{Float64,2,Array{Float64,2},(StepRange{Int64,Int64},StepRange{Int64,Int64})}:
 0.537192  0.996234
 0.736979  0.228787
 0.991511  0.74485
 0.836126  0.0224702

julia&gt; (q,r) = qr(b);

julia&gt; q
4x2 Array{Float64,2}:
 -0.338809   0.78934
 -0.464815  -0.230274
 -0.625349   0.194538
 -0.527347  -0.534856

julia&gt; r
2x2 Array{Float64,2}:
 -1.58553  -0.921517
  0.0       0.866567</pre>
</div>
</div>
</div>
<div class="section" id="sparse-matrices">
<h2>Sparse Matrices<a class="headerlink" href="#sparse-matrices" title="Permalink to this headline">¶</a></h2>
<p><a class="reference external" href="http://en.wikipedia.org/wiki/Sparse_matrix">Sparse matrices</a> are
matrices that contain enough zeros that storing them in a special data
structure leads to savings in space and execution time. Sparse
matrices may be used when operations on the sparse representation of a
matrix lead to considerable gains in either time or space when
compared to performing the same operations on a dense matrix.</p>
<div class="section" id="compressed-sparse-column-csc-storage">
<h3>Compressed Sparse Column (CSC) Storage<a class="headerlink" href="#compressed-sparse-column-csc-storage" title="Permalink to this headline">¶</a></h3>
<p>In Julia, sparse matrices are stored in the <a class="reference external" href="http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29">Compressed Sparse Column
(CSC) format</a>.
Julia sparse matrices have the type <tt class="docutils literal"><span class="pre">SparseMatrixCSC{Tv,Ti}</span></tt>, where <tt class="docutils literal"><span class="pre">Tv</span></tt>
is the type of the nonzero values, and <tt class="docutils literal"><span class="pre">Ti</span></tt> is the integer type for
storing column pointers and row indices.:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="k">type</span><span class="nc"> SparseMatrixCSC</span><span class="p">{</span><span class="n">Tv</span><span class="p">,</span><span class="n">Ti</span><span class="o">&lt;:</span><span class="n">Integer</span><span class="p">}</span> <span class="o">&lt;:</span> <span class="n">AbstractSparseMatrix</span><span class="p">{</span><span class="n">Tv</span><span class="p">,</span><span class="n">Ti</span><span class="p">}</span>
    <span class="n">m</span><span class="p">::</span><span class="kt">Int</span>                  <span class="c"># Number of rows</span>
    <span class="n">n</span><span class="p">::</span><span class="kt">Int</span>                  <span class="c"># Number of columns</span>
    <span class="n">colptr</span><span class="p">::</span><span class="n">Vector</span><span class="p">{</span><span class="n">Ti</span><span class="p">}</span>      <span class="c"># Column i is in colptr[i]:(colptr[i+1]-1)</span>
    <span class="n">rowval</span><span class="p">::</span><span class="n">Vector</span><span class="p">{</span><span class="n">Ti</span><span class="p">}</span>      <span class="c"># Row values of nonzeros</span>
    <span class="n">nzval</span><span class="p">::</span><span class="n">Vector</span><span class="p">{</span><span class="n">Tv</span><span class="p">}</span>       <span class="c"># Nonzero values</span>
<span class="k">end</span>
</pre></div>
</div>
<p>The compressed sparse column storage makes it easy and quick to access
the elements in the column of a sparse matrix, whereas accessing the
sparse matrix by rows is considerably slower. Operations such as
insertion of nonzero values one at a time in the CSC structure tend to
be slow. This is because all elements of the sparse matrix that are
beyond the point of insertion have to be moved one place over.</p>
<p>All operations on sparse matrices are carefully implemented to exploit
the CSC data structure for performance, and to avoid expensive operations.</p>
<p>If you have data in CSC format from a different application or library,
and wish to import it in Julia, make sure that you use 1-based indexing.
The row indices in every column need to be sorted. If your <cite>SparseMatrixCSC</cite>
object contains unsorted row indices, one quick way to sort them is by
doing a double transpose.</p>
<p>In some applications, it is convenient to store explicit zero values
in a <cite>SparseMatrixCSC</cite>. These <em>are</em> accepted by functions in <tt class="docutils literal"><span class="pre">Base</span></tt>
(but there is no guarantee that they will be preserved in mutating
operations).  Such explicitly stored zeros are treated as structural
nonzeros by many routines.  The <a class="reference internal" href="../stdlib/sparse.html#Base.nnz" title="Base.nnz"><tt class="xref jl jl-func docutils literal"><span class="pre">nnz()</span></tt></a> function returns the number of
elements explicitly stored in the sparse data structure,
including structural nonzeros. In order to count the exact number of actual
values that are nonzero, use <a class="reference internal" href="../stdlib/base.html#Base.countnz" title="Base.countnz"><tt class="xref jl jl-func docutils literal"><span class="pre">countnz()</span></tt></a>, which inspects every stored
element of a sparse matrix.</p>
</div>
<div class="section" id="sparse-matrix-constructors">
<h3>Sparse matrix constructors<a class="headerlink" href="#sparse-matrix-constructors" title="Permalink to this headline">¶</a></h3>
<p>The simplest way to create sparse matrices is to use functions
equivalent to the <a class="reference internal" href="../stdlib/base.html#Base.zeros" title="Base.zeros"><tt class="xref jl jl-func docutils literal"><span class="pre">zeros()</span></tt></a> and <a class="reference internal" href="../stdlib/base.html#Base.eye" title="Base.eye"><tt class="xref jl jl-func docutils literal"><span class="pre">eye()</span></tt></a> functions that Julia provides
for working with dense matrices. To produce sparse matrices instead,
you can use the same names with an <tt class="docutils literal"><span class="pre">sp</span></tt> prefix:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">spzeros</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="mi">3</span><span class="n">x5</span> <span class="n">sparse</span> <span class="n">matrix</span> <span class="n">with</span> <span class="mi">0</span> <span class="kt">Float64</span> <span class="n">entries</span><span class="p">:</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">speye</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="mi">3</span><span class="n">x5</span> <span class="n">sparse</span> <span class="n">matrix</span> <span class="n">with</span> <span class="mi">3</span> <span class="kt">Float64</span> <span class="n">entries</span><span class="p">:</span>
        <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
        <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
        <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../stdlib/sparse.html#Base.sparse" title="Base.sparse"><tt class="xref jl jl-func docutils literal"><span class="pre">sparse()</span></tt></a> function is often a handy way to construct sparse
matrices. It takes as its input a vector <tt class="docutils literal"><span class="pre">I</span></tt> of row indices, a
vector <tt class="docutils literal"><span class="pre">J</span></tt> of column indices, and a vector <tt class="docutils literal"><span class="pre">V</span></tt> of nonzero
values. <tt class="docutils literal"><span class="pre">sparse(I,J,V)</span></tt> constructs a sparse matrix such that
<tt class="docutils literal"><span class="pre">S[I[k],</span> <span class="pre">J[k]]</span> <span class="pre">=</span> <span class="pre">V[k]</span></tt>.</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">I</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">];</span> <span class="n">J</span> <span class="o">=</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">9</span><span class="p">];</span> <span class="n">V</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">];</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">S</span> <span class="o">=</span> <span class="n">sparse</span><span class="p">(</span><span class="n">I</span><span class="p">,</span><span class="n">J</span><span class="p">,</span><span class="n">V</span><span class="p">)</span>
<span class="mi">5</span><span class="n">x18</span> <span class="n">sparse</span> <span class="n">matrix</span> <span class="n">with</span> <span class="mi">4</span> <span class="kt">Int64</span> <span class="n">entries</span><span class="p">:</span>
        <span class="p">[</span><span class="mi">1</span> <span class="p">,</span>  <span class="mi">4</span><span class="p">]</span>  <span class="o">=</span>  <span class="mi">1</span>
        <span class="p">[</span><span class="mi">4</span> <span class="p">,</span>  <span class="mi">7</span><span class="p">]</span>  <span class="o">=</span>  <span class="mi">2</span>
        <span class="p">[</span><span class="mi">5</span> <span class="p">,</span>  <span class="mi">9</span><span class="p">]</span>  <span class="o">=</span>  <span class="mi">3</span>
        <span class="p">[</span><span class="mi">3</span> <span class="p">,</span> <span class="mi">18</span><span class="p">]</span>  <span class="o">=</span>  <span class="o">-</span><span class="mi">5</span>
</pre></div>
</div>
<p>The inverse of the <a class="reference internal" href="../stdlib/sparse.html#Base.sparse" title="Base.sparse"><tt class="xref jl jl-func docutils literal"><span class="pre">sparse()</span></tt></a> function is <a class="reference internal" href="../stdlib/base.html#Base.findn" title="Base.findn"><tt class="xref jl jl-func docutils literal"><span class="pre">findn()</span></tt></a>, which
retrieves the inputs used to create the sparse matrix.</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">findn</span><span class="p">(</span><span class="n">S</span><span class="p">)</span>
<span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">],[</span><span class="mi">4</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">18</span><span class="p">])</span>

<span class="n">julia</span><span class="o">&gt;</span> <span class="n">findnz</span><span class="p">(</span><span class="n">S</span><span class="p">)</span>
<span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">],[</span><span class="mi">4</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">18</span><span class="p">],[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="o">-</span><span class="mi">5</span><span class="p">])</span>
</pre></div>
</div>
<p>Another way to create sparse matrices is to convert a dense matrix
into a sparse matrix using the <a class="reference internal" href="../stdlib/sparse.html#Base.sparse" title="Base.sparse"><tt class="xref jl jl-func docutils literal"><span class="pre">sparse()</span></tt></a> function:</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">sparse</span><span class="p">(</span><span class="n">eye</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="mi">5</span><span class="n">x5</span> <span class="n">sparse</span> <span class="n">matrix</span> <span class="n">with</span> <span class="mi">5</span> <span class="kt">Float64</span> <span class="n">entries</span><span class="p">:</span>
        <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
        <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
        <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
        <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
        <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>  <span class="o">=</span>  <span class="mf">1.0</span>
</pre></div>
</div>
<p>You can go in the other direction using the <a class="reference internal" href="../stdlib/sparse.html#Base.full" title="Base.full"><tt class="xref jl jl-func docutils literal"><span class="pre">full()</span></tt></a> function. The
<a class="reference internal" href="../stdlib/sparse.html#Base.issparse" title="Base.issparse"><tt class="xref jl jl-func docutils literal"><span class="pre">issparse()</span></tt></a> function can be used to query if a matrix is sparse.</p>
<div class="highlight-julia"><div class="highlight"><pre><span class="n">julia</span><span class="o">&gt;</span> <span class="n">issparse</span><span class="p">(</span><span class="n">speye</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="n">true</span>
</pre></div>
</div>
</div>
<div class="section" id="sparse-matrix-operations">
<h3>Sparse matrix operations<a class="headerlink" href="#sparse-matrix-operations" title="Permalink to this headline">¶</a></h3>
<p>Arithmetic operations on sparse matrices also work as they do on dense
matrices. Indexing of, assignment into, and concatenation of sparse
matrices work in the same way as dense matrices. Indexing operations,
especially assignment, are expensive, when carried out one element at
a time. In many cases it may be better to convert the sparse matrix
into <tt class="docutils literal"><span class="pre">(I,J,V)</span></tt> format using <a class="reference internal" href="../stdlib/base.html#Base.findnz" title="Base.findnz"><tt class="xref jl jl-func docutils literal"><span class="pre">findnz()</span></tt></a>, manipulate the non-zeroes or
the structure in the dense vectors <tt class="docutils literal"><span class="pre">(I,J,V)</span></tt>, and then reconstruct
the sparse matrix.</p>
</div>
<div class="section" id="correspondence-of-dense-and-sparse-methods">
<h3>Correspondence of dense and sparse methods<a class="headerlink" href="#correspondence-of-dense-and-sparse-methods" title="Permalink to this headline">¶</a></h3>
<p>The following table gives a correspondence between built-in methods on sparse
matrices and their corresponding methods on dense matrix types. In general,
methods that generate sparse matrices differ from their dense counterparts in
that the resulting matrix follows the same sparsity pattern as a given sparse
matrix <tt class="docutils literal"><span class="pre">S</span></tt>, or that the resulting sparse matrix has density <tt class="docutils literal"><span class="pre">d</span></tt>, i.e. each
matrix element has a probability <tt class="docutils literal"><span class="pre">d</span></tt> of being non-zero.</p>
<p>Details can be found in the <a class="reference internal" href="../stdlib/sparse.html#stdlib-sparse"><em>Sparse Matrices</em></a> section of the standard library
reference.</p>
<table border="1" class="docutils">
<colgroup>
<col width="34%" />
<col width="29%" />
<col width="37%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Sparse</td>
<td>Dense</td>
<td>Description</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/sparse.html#Base.spzeros" title="Base.spzeros"><tt class="xref jl jl-func docutils literal"><span class="pre">spzeros(m,n)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.zeros" title="Base.zeros"><tt class="xref jl jl-func docutils literal"><span class="pre">zeros(m,n)</span></tt></a></td>
<td>Creates a <em>m</em>-by-<em>n</em> matrix of zeros.
(<a class="reference internal" href="../stdlib/sparse.html#Base.spzeros" title="Base.spzeros"><tt class="xref jl jl-func docutils literal"><span class="pre">spzeros(m,n)</span></tt></a> is empty.)</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/sparse.html#Base.spones" title="Base.spones"><tt class="xref jl jl-func docutils literal"><span class="pre">spones(S)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.ones" title="Base.ones"><tt class="xref jl jl-func docutils literal"><span class="pre">ones(m,n)</span></tt></a></td>
<td>Creates a matrix filled with ones.
Unlike the dense version, <a class="reference internal" href="../stdlib/sparse.html#Base.spones" title="Base.spones"><tt class="xref jl jl-func docutils literal"><span class="pre">spones()</span></tt></a>
has the same sparsity pattern as <em>S</em>.</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/sparse.html#Base.speye" title="Base.speye"><tt class="xref jl jl-func docutils literal"><span class="pre">speye(n)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.eye" title="Base.eye"><tt class="xref jl jl-func docutils literal"><span class="pre">eye(n)</span></tt></a></td>
<td>Creates a <em>n</em>-by-<em>n</em> identity matrix.</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/sparse.html#Base.full" title="Base.full"><tt class="xref jl jl-func docutils literal"><span class="pre">full(S)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/sparse.html#Base.sparse" title="Base.sparse"><tt class="xref jl jl-func docutils literal"><span class="pre">sparse(A)</span></tt></a></td>
<td>Interconverts between dense
and sparse formats.</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/sparse.html#Base.sprand" title="Base.sprand"><tt class="xref jl jl-func docutils literal"><span class="pre">sprand(m,n,d)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.rand" title="Base.rand"><tt class="xref jl jl-func docutils literal"><span class="pre">rand(m,n)</span></tt></a></td>
<td>Creates a <em>m</em>-by-<em>n</em> random matrix (of
density <em>d</em>) with iid non-zero elements
distributed uniformly on the
interval [0, 1].</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/sparse.html#Base.sprandn" title="Base.sprandn"><tt class="xref jl jl-func docutils literal"><span class="pre">sprandn(m,n,d)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.randn" title="Base.randn"><tt class="xref jl jl-func docutils literal"><span class="pre">randn(m,n)</span></tt></a></td>
<td>Creates a <em>m</em>-by-<em>n</em> random matrix (of
density <em>d</em>) with iid non-zero elements
distributed according to the standard
normal (Gaussian) distribution.</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../stdlib/sparse.html#Base.sprandn" title="Base.sprandn"><tt class="xref jl jl-func docutils literal"><span class="pre">sprandn(m,n,d,X)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.randn" title="Base.randn"><tt class="xref jl jl-func docutils literal"><span class="pre">randn(m,n,X)</span></tt></a></td>
<td>Creates a <em>m</em>-by-<em>n</em> random matrix (of
density <em>d</em>) with iid non-zero elements
distributed according to the <em>X</em>
distribution. (Requires the
<tt class="docutils literal"><span class="pre">Distributions</span></tt> package.)</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../stdlib/sparse.html#Base.sprandbool" title="Base.sprandbool"><tt class="xref jl jl-func docutils literal"><span class="pre">sprandbool(m,n,d)</span></tt></a></td>
<td><a class="reference internal" href="../stdlib/base.html#Base.randbool" title="Base.randbool"><tt class="xref jl jl-func docutils literal"><span class="pre">randbool(m,n)</span></tt></a></td>
<td>Creates a <em>m</em>-by-<em>n</em> random matrix (of
density <em>d</em>) with non-zero  <tt class="docutils literal"><span class="pre">Bool</span></tt>
elements with probability <em>d</em> (<em>d</em> =0.5
for <a class="reference internal" href="../stdlib/base.html#Base.randbool" title="Base.randbool"><tt class="xref jl jl-func docutils literal"><span class="pre">randbool()</span></tt></a>.)</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>


          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="linear-algebra.html" class="btn btn-neutral float-right" title="Linear algebra"/>Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="metaprogramming.html" class="btn btn-neutral" title="Metaprogramming"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
    </p>
  </div>

  <a href="https://github.com/snide/sphinx_rtd_theme">Sphinx theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>
</footer>
        </div>
      </div>

    </section>

  </div>
  

  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'0.3.4',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>
      <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>