Julia Programming Language - Tutorials Point

Transcription

Julia Programming1

Julia ProgrammingAbout the TutorialOne of the facts about scientific programming is that it requires high performance flexibledynamic programming language. Unfortunately, to a great extent, the domain expertshave moved to slower dynamic programming languages. There can be many good reasonsfor using such dynamic programming languages and, in fact, their use cannot bediminished as well. On the flip side, what can we expect from modern language designand compiler techniques? Some of the expectations are as follows: It should eradicate the performance trade-off. It should provide the domain experts a single environment that is productiveenough for prototyping and efficient for deploying performance-intensiveapplications.The Julia programming language fulfill these expectations. It is a general purpose highperformance flexible programming language which can be used to write any applications.It is well-suited for scientific and numerical computing.AudienceThis tutorial will be useful for graduates, post-graduates, and research students who eitherhave an interest in Julia Programming or have these subjects as a part of their curriculum.The reader can be a beginner or an advanced learner.PrerequisitesThe reader should have knowledge on basic computer programming languages.Copyright & Disclaimer Copyright 2020 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com2

Julia ProgrammingTable of ContentsAbout the Tutorial . 2Audience . 2Prerequisites . 2Copyright & Disclaimer . 2Table of Contents . 31.Julia — Overview . 10What is Julia Programming Language? . 10History of Julia . 10Features of Julia . 11The Scope of Julia . 11Comparison with other languages . 122.Julia Programming — Environment Setup . 13Installing Julia . 13Julia’s working environment. 15Packages . 16Installing IJulia . 20Installing Juno . 213.Julia Programming — Basic Syntax . 22Variables . 22Comments . 234.Julia — Arrays . 24Creating Simple 1D Arrays . 24Creating 2D arrays & matrices . 26Creating arrays using range objects . 26Creating arrays using comprehensions and generators . 29Populating an Array . 303

Julia ProgrammingArray Constructor . 32Arrays of arrays . 33Copying arrays . 33Matrix Operations . 33Accessing the contents of arrays . 34Adding Elements . 36Removing Elements . 385.Julia — Tuples . 41Creating tuples . 41Named tuples . 42Creating named tuples . 42Named tuples as keyword arguments . 446.Julia — Integers and Floating-Point Numbers . 45Integers . 45Floating-point numbers . 47Special floating-point values . 497.Julia — Rational and Complex Numbers. 51Rational Numbers . 51Complex Numbers . 528.Julia — Basic Operators . 55Arithmetic Operators. 55Bitwise Operators . 56Updating Operators . 57Vectorized “dot” Operators . 58Numeric Comparisons Operators . 59Chaining Comparisons . 60Operator Precedence & Associativity . 619.Julia — Basic Mathematical Functions . 634

Julia ProgrammingNumerical Conversions . 63Rounding functions. 64Division functions . 66Sign and Absolute value functions . 68Power, Logs, and Roots . 70Trigonometric and hyperbolic functions . 7310. Julia — Strings. 75Characters. 75Unicode and UTF-8 . 77String Concatenation . 78Interpolation . 79Triple-quoted strings . 79Common String Operations . 80Non-standard String Literals . 8111. Julia — Functions . 84Defining Functions . 84Optional Arguments . 85Keyword Arguments . 86Anonymous Functions . 87Recursive Functions . 87Map . 88Filter . 89Generic Functions . 89Multiple dispatch . 8912. Julia — Flow Control . 91Ternary expressions. 91Boolean Switching expressions . 91If, elseif and else . 925

Julia Programmingfor loops . 93Loop variables. 94Variables declared inside a loop . 95Continue Statement . 96Comprehensions . 97Enumerated arrays . 97Zipping arrays . 98Nested loops . 99While loops . 100Exceptions . 101Do block . 10113. Julia — Dictionaries and Sets . 103Creating Dictionaries . 103Keys . 104Values . 105Sorting a dictionary . 106Word Counting Example . 107Sets . 111Standard operations . 112Some Functions on Dictionary . 11414. Julia — Date & Time . 116Relationship between Types . 116Date, Time, and DateTimes . 118Queries regrading Date and Time. 118Date Arithmetic . 119Range of Dates. 120Formatting of Dates . 121Rounding Dates and Times . 1226

Julia ProgrammingRecurring Dates . 122Unix time . 123Moments in time . 124Time and Monitoring . 12415. Julia — Files I/O . 125Reading from files . 125Reading a file all at once . 126Reading line by line . 126Path and File Names . 127Information about file . 128Interacting with the file system . 129Writing to files . 12916. Julia Programming — Metaprogramming . 131Quoted expressions . 131Evaluated expressions . 132The Abstract Syntax Tree (AST) . 133Expression interpolation. 133Macros . 134Expanding Macros . 13517. Julia Programming — Plotting . 136Plotting a function . 136Packages . 137VegaLite . 13918. Julia Programming — Data Frames . 141Loading data into DataFrames . 141Collected Datasets . 142Empty DataFrames . 143Plotting Anscombe’s Quarter . 1447

Julia ProgrammingRegression and Models . 147Working with DataFrames . 148Handling missing values . 149Looking for missing values . 150Repairing DataFrames . 150Working with missing values . 151Modifying DataFrames . 152Finding values in DataFrame . 15519. Julia Programming — Working with Datasets . 156CSV files . 156HDF5 . 158XML files . 160RDatasets . 162Statistics in Julia . 164Descriptive Statistics. 165Samples and Estimations . 16620. Julia Programming — Modules and Packages . 169Installing Modules . 169Packages . 17121. Julia Programming — Working with Graphics . 174Text Plotting with . 174Cairo . 174Text Plotting with Winston . 175Data Visualization . 176Gadfly . 177Compose . 180Graphics Engines . 181Gaston . 1838

Julia ProgrammingPGF Plots . 18522. Julia Programming — Networking. 187Sockets and Servers . 187Well-known ports . 187Julia’s UDP and TCP sockets . 187A TCP web service. 189The Julia Web Group . 190WebSockets . 191Messaging . 191Cloud Services. 193The Google Cloud . 19423. Julia Programming — Databases .

new era of technical computing where they can develop libraries in a high-level programming language. Following is the detailed comparison of Julia with the most used programming languages — Matlab, R, and Python: MATLAB: The syntax of Julia is similar to MATLAB, however it is a much general purpose