Spring 2025

Introduction to Julia

What is Julia?

  • A fourth generation language designed from the ground up with scientists in mind
    • Highly performant, capable of parallelism by default
    • Perhaps both dynamic and static type binding
    • Easy to read and code in, like R and Python
  • Advantages:
    • Very fast
    • Can call C, R, and Python natively without wrappers
    • Can create compact, efficient user-defined types like C
    • Still get the interactive, console experience from R and Python

Install the Latest Julia

The latest version of Julia as of this presentation is 1.11.4

  1. Go to https://julialang.org/
  2. Select the Download tab
  3. Then download the install that that matches your platform
  4. Follow the download and install prompts

The Julia Console

  • You can also run Julia interactively
  • If it is installed and in path, just type julia at the command line
  • You’ll be give a prompt that likes like this: julia>
  • You can run commands interactively from that prompt
print("Julia is easy!")
  • Later we’ll talk about using scripts to do more complicated things

Variables & Assignment

  • Julia also stores values in variables
  • Like R and Python, Julia is dynamically bound, meaning the type of a variable is determined at runtime when values are assigned (i.e., not declared)
  • Assignment in Julia uses the = operator
my_var = 32

Math Operations

  • The standard arithmetic operators more or less work as expected:
  • +, -, *, /, (, )
  • Julia also has an exponentiation operator: ^
x = 2^10 + (3*5 + 1)/2
print(x)

What is a Julia Soure File?

  • Like R and Python, you can type commands into the console

  • But for anything even moderately sophisticated, you’ll probably want to create a source file

  • A Julia source file is a text-readable file with Julia commands in them that can be run any time

  • There are many advantages, not the least of which is being able to give the file to someone else to run

  • Also, you’ll need to turn in a source file for homework

  • Julia source files typically end in .jl

Some Basic Data Types

  • floating point numbers
  • integer numbers
  • strings
  • Booleans
  • lists

Static vs. Dynamic Type Binding

  • Julia can dynamically bind type, like R and Python
  • But you can also explicitly specify the type
  • And you can define your own types
myboundvar::Int = 11
myboundvar = 3.5   # Will produce error

struct myt
  a::Float64
  b::String
end

list = [myt(2,"toad"), "Foo", myt(-3.2, "Purple")]
list[1].b   # Julia is 1-indexed, not 0-indexed
list[3].a

Numeric Data

  • int are integers
  • float are floating point numbers
  • Like Python, Julia usually coerces to float
x = 3
y = 4.7
x/y + 2*x - 3

String Data

  • You may use double quotes or single quotes, but they must match
  • Julia has many convenient string operations
z = "   Hello There "
length(z)
print("My string was: '$z'")
print( lowercase(z) )
split("This; is; a test of; split")

Concatenating Strings

x = "hello"
z = "world"
print("$x  $z")

Lists

  • Julia lists can be dynamically expanded or reduced
  • They can contain different types of data
  • Are one-indexed
x = [12, -3, "no", True, ['a', 'b', 'c']]
x[1]
x[end-1]

Tuples

  • Like Python, Julia has tuples
myVar = (1, 2, "Hello")
println(myVar)

Immutable Structures

  • Julia allows you to create types with named fields (i.e., structures)
  • By default, these variables are immutable, so they are a little named tuples
struct FooType1
         a::Int64
         b::Float64
       end
foo = FooType1(2, -6.3)
foo.a
foo.a = 3  # This will give an error

Mutable Structures

  • But you can tell Julia the structure is mutable
mutable struct FooType2
         a::Int64
         b::Float64
       end
foo = FooType2(2, -6.3)
foo.a
foo.a = 3  # This will not give an error

Dictionaries

  • Julia has dictionaries, just like Python
  • But Julia allows them to be typed, if you like
Dict2 = Dict("a" => 1, "b" => 2, "c" => 3) 
println("\nUntyped Dictionary = ", Dict2) 
  
Dict3 = Dict{String, Integer}("a" => 64, "c" => 20) 
println("\nTyped Dictionary = ", Dict3) 

Conditionals:

if x < y
    println("x is less than y")
elseif x > y
    println("x is greater than y")
else
    println("x is equal to y")
end

Simple for Loops

for i in 1:10
    println("Index: $i")
end

Looping Through Dictionaries

Dict2 = Dict("a" => 1, "b" => 2, "c" => 3) 
for item in Dict3
    println("Item: $item")
end

List Comprehensions

  • Julia provides short-hand ways to build lists
cubes = [z^3  for z in 1:10]
println(cubes)

Defining a Function

# Typical Way
function myfunction(arg)
  argsq = arg^2
  return argsq
end

# Compact Way
otherfunction(x, y) = 2*x + y

# Calling functions
myfunction(3)
otherfunction(-2, 3)

Loading External Packages

  • There are many external pacakges available to install for Julia
  • Common ones that we’ll use include: DataFrames, Plots, SciML, JuliaStats
  • We load packages with using and install with Pkg.add
using Pkg
Pkg.add("Plots")
using Plots
x = 0:.1:10
z = sin.(x)
plot(x,z)

Learn More