Skip to content
Paul Barrett edited this page Dec 18, 2025 · 82 revisions

Welcome to the AAS247Julia wiki, created 16 September 2025.

Members

  1. Paul Barrett
  2. Erandi Chavez
  3. Denis Cioffi
  4. Abhro Rahman
  5. Ian Weaver

Outline

N.B. Session lectures should be about 15 minutes, with the rest of the time for the students to work.

FYI: "Typically, it takes about 10 minutes for GitHub Pages to build and deploy the changes."


Day 1

1. Introduction

  • Contextual history of Python and Julia
  • Installation, if necessary
  • Workshop outline
  • Package management
    • environments and their relationship to projects (Day 2; show Day 2 outline, too.)
    • quick note of DrWatson.jl package
  • A Julia sampler
    • Measurements example and problems
    • Unitful example and problems
    • Symbol example and problems
    • GPU example and problem
  • 7+ problems (~5 minutes each)
    • Create the variable ϕ₀
    • Add the Statistics package
    • Calculate the mean of a Normal distribution with a mean of 10
    • Add the FITSFiles package
    • Open a BinaryTable FITS file
    • Add the Plots package
    • Plot the light curve from the BinaryTable

2. Arrays 1

  • Multidimensional Arrays

    • Mathematics protocol, i.e., column major & 1 indexing
    • Array creation, i.e., ',' & ';' syntax
    • Array broadcasting, i.e., dot (.) operator
    • Array fusion (A.*B .+ C), i.e., implied for loop
    • Matrix multiplication (* operator)
  • 7+ problems (~5 minutes each)

    • Column major vs Row major protocol
    • 1 indexing vs 0 indexing
    • Non-1 indexing using OffsetArrays.jl
    • Create 1D arrays using ','
    • Create 2D arrays using ' ' and ';'
    • Create 2D array from 1D array using reshape
    • Create 3D array
    • Array broadcasting of exponentiation '.^'
    • Array broadcasting using @. macro
    • Array fusion performance vs Python
    • Vector-Matrix multiplication
    • Matrix-Matrix multiplication
    • Virtual identity matrix

3. Arrays 2

  • Array and tuple comprehensions, i.e., [... for ...], (... for ...)

  • Array comprehensions with "if statement"

  • Array slicing & indexing, i.e., [1:2:end], [1:2:10]

  • Array slicing with boolean array

  • 7+ problems (~5 minutes each)

    • Create a 10x10 identity matrix I1
      • Hint: use an array comprehension
    • Display the size of the identity matrix I1
      • Hint: sizeof(I1)
    • Add the LinearAlgebra package
    • Create a 10x10 identity matrix I2
    • Display the size of the identity matrix I2
    • Cartesian Index

4. Functions

  • Defining functions

    • Multiline function, e.g., function myfunc(x) ... end
    • Single-line function, e.g., myfunc(x) = ...
    • Anonymous function, e.g., (x) -> ...
  • Function arguments and keywords

    • ';' separates arguments and keywords in function definition, e.g., myfunc(arg1, arg2; keywd1, keywd2)
    • Arguments can be modified by types, e.g., myfunc(arg1::Int32, arg2::Float64)
    • Julia will infer the argument type using the value type
    • Default values for arguments, e.g., myfunc(arg1, arg2=0)
    • Functions can have multiple methods, e.g., myfunc(arg1), myfunc(arg1, arg2)
  • Multiple dispatch

    • The difference between Julia and Python
    • Object-oriented languages use single dispatch, i.e., function/method is chosen using only the first argument Note: this is the reason for forward and reverse operators/functions, i.e., __add__() and __radd__()
    • Julia uses multiple dispatch, i.e., function is chosen using all arguments (but not keywords)
      • Note: +(a::Int32, b::Float32) and +(a::Float32, b::Int32)
      • Note: Multiple dispatch is a powerful language feature.
  • 7+ problems (~5 minutes each)

    • Define and use a multi-line function without keywords.
    • Define and use a multi-line function with default values and without keywords.
    • Define and use a multi-line function with default values and keywords.
    • Define and use a single-line function without keywords.
    • Define and use a single-line function with keywords.
    • Define and use an anonymous function.
    • Use an anonymous function with the filter() and map().
    • Define an add() with no argument types and use it with different argument types.
    • Use methods() to show the add() methods.
    • Define an add() with specified argument types and use it.
      • Note: Julia will use the specific version function before defaulting to the general version function.

5. Types

  • Type declarations

    • '::' operator append to the variable and function specifies its type, e.g., a::Int32, myfunc()::Float32
  • Type hierarchy or graph

    • Integer subtypes, i.e., Int8, UInt8, Int16, UInt16, ...
  • The types of Types

    • Concrete types, i.e., Int8, Float32, ... , e.g., a::Int8, b::Float32 Note: concrete types can be instantiated, i.e., they are usually associated with a machine type
    • Abstract types, i.e., Integer, AbstractFloat, AbstractArray, e.g., a::Integer, b::AbstractFloat Note: abstract types cannot be instantiated. They are a node in the type hierarchy or graph
    • Parametric types, i.e., type1{type2}, e.g., AbstractVector{Float32}, AbstractArray{Integer}
    • Composite types, e.g.,
      struct MyType
          field1::Int32
          field2::Float64
          field3::Integer
          field4::AbstractFloat
      end
      • Note: composite types should begin with a capital.
      • Note: a 'struct' is its on constructor, e.g., MyType(Int32(8), 1.0, 2, 10f0) will instantiate the composite type
    • Parametric types
      struct MyType{T}
          field1::T
          field2::T
      end
    • Union types, e.g., Union{Integer, Missing}
    • Singleton types have only one instance, e.g., Missing, Nothing
  • 7+ problems (~5 minutes each)

    • Show the subtype graph for the Integer abstract type, e.g., subtypes(Integer).
    • Show the subtype graph for the AbstractFloat abstract type.
    • Show the subtype graph for the Real abstract type.
    • Show the supertype for Real.
    • Show the supertype for Number.
    • Show the supertype for Any.
    • Where is Any in the type graph?
    • Create a 1D array or vector and show its type, i.e., typeof(vector). Note: What kind of type is it?
    • Create a composite type, e.g., struct AType a::Integer b::String end, and instantiate it, i.e., AType(2, "abc").

6. Function and Types

  • Functions and abstract types

    • Let Julia write and compile the code
    • myfunc(a::Float32) and myfunc(a::Float64) require a version of the function for each argument type
    • For statically compiled languages, the coder has to write each version
    • For Julia, the language can write it for you
    • This is what abstract types are for, e.g., myfunc(a::AbstractFloat)
    • This tells Julia to constrain the argument type a floating point value, integers are not allowed
  • Functions can be constructors for composite types

    • E.g., a function can be used to create a one argument version of a composite type
    • struct MyType a::Integer b::Integer end
    • function MyType(a, b=0) = MyType(a, b)
    • MyType(1)
  • Object-oriented behaviour

    • The first argument of an Object-Oriented method is the class
      • For Python, mymethod(self, a, b)
    • The same is done for Julia
      • For Julia, mymethod(mytype, a, b)
    • Note: Julia can mimic object-oriented languages
    • Note: Object-oriented languages tightly couple types and functions (methods)
    • Note: Julia loosely couples types and functions
  • Functors

    • Functors are nameless functions. They are defined by their argument type, usually a composite type
      • Construct the polynomial type
        struct Polynomial{R}
            coef::Vector{R}
        end
      • Construct the function to evaluate the polynomial
        function (p::Polynomial)(x)
            ...
        end
      • Create the polynomial
        p = Polynomial([1, 2, 3])
      • Evaluate the polynomial
        p(3)
  • 7+ problems (~5 minutes each)

    • Create a function with a default type for 'b', e.g., AType(a, b="") = AType(a, b), and instantiate using the function.

Day 2

7. PythonCall or Transitioning to Julia

  • PythonCall
  • how does one translate code from one language to the next
  • have students do group coding to translate code from Python to Julia, compare+contrast+discuss between all groups at the end

8. Using Macros

9. Optimization

10. Parallel Processing (including GPUs)

11. Creating Packages

12. JuliAstro

What references should we list? recommend?

Textual

Video

Specific Julia Sites of Interest

A new package, Swirl

==========================================

  • Using Arrays with examples loading data examples
  • loading data from a CSV file
  • loading data from a text file
  • Power of Julia with Arrays: Broadcasting and Fusion
  • broadcasting as an implied for loop
  • operations fused - huge benefit for performance
  • show some benchmarks to emphasize optimization
  • conciseness
  • indexing and slicing
  • don't need the NumPy overhead
  • Quality of life improvements
    • General interactions with arrays, and the benefits that comes from it being built-in to the language
  • list & array comprehension
  • Difference between element-by-element multiplication (.*) and matrix multiplication(*)
  • tuples have less allocation: faster (can show benchmarks as exercise?) Let them play around with arrays while emphasizing the above points!
  • Broadcasting with functions leads into function section, below.

MOVE. To ...? Does a brief discussion of AI go here? Point should be made that AI doesn't know Julia well, and so its specific commands will often be incorrect, but it can still give good ideas on how to proceed.

  • Perhaps come with a packaged example.

Symbolics? Where?