Basics of Julia & Numerical Computation - Part 3

Basics of Julia & Numerical Computation - Part 3

After reading this article, my goal is that you'll be able to:

  • Get input data
  • Convert between data types
  • Perform element-wise operations using the "dot" operator
  • Carry out numeric comparisons in julia.

This is a continuation of Basics of Julia & Numerical Computation parts 1 and 2. If you missed the earlier parts, I'd encourage you to read them before continuing this article.

Let's delve in.

Getting Input From I/O Stream

To get input from a user, readline() function is used. It reads a line of text from standard input (STDIN) until a newline character is entered.

In plain speech, this method reads text until the enter key is pressed.

Values returned by readline() are always Strings.

E.g.

# get the user's name
name = readline()
  • In a case where multiple lines are required to be entered by a user, the readlines() method is used.
  • The lines are stored as a one-dimensional String array.
  • A new line separates each line and CTRL-D is pressed to stop taking input.

E.g.

# tell us about yourself
info = readlines()

Converting Between Data Types

  • In julia, there are fundamentally three ways to convert between data types: parse(), convert() and string() functions.

Parse()

  • parse() resolves a string into a specified data type. If the string does not contain valid data for the type chosen, julia will raise an error.
# SYNTAX
parse(type, string)

Converting String into Int

ss1.png

Converting String into Float and sample of error thrown for invalid data type

ss2.png

Convert()

  • convert() resolves a value from Int to Float and vice versa, provided that it is a valid representation of the data type being converted to.
# SYNTAX:
convert(type, value)

# tests
convert(Int64, 3.0)       # returns 3
convert(Int64, 3.5)       # returns an error
convert(Float64, 3.4)     # returns 3.4

String()

string() function creates a string from any value.

E.g.

string(23.4)     # returns "23.4"

The "dot"(.) operator

  • In scientific computing, it is common to work with large datasets and most of the time, one would want to perform element-wise operations on them.

  • The significance of element-wise operations include:

    • Performing a distributed operation over an array of numbers.
    • Used in developing neural networks.
    • Finding squared errors (which is later used to compute the cost function, J(θ) )

And I'm sure there's a lot more to discover.

  • The dot(.) before mathematical operators in julia are used to perform element-wise computation on arrays or matrices.
  • The "dot" operator can be used with any mathematical operator.
# Samples
[1, 2, 3] .* 5     # multiplies all the elements by 5.

[
    1 3 4
    2 3 4
] ./ 2             # divides all elements by 2

Numeric Comparisons

The following is a list of comparison operations in julia for numeric data types

SymbolMeaning
==Equality
!=Not equals
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
===Strict comparison operator (Takes data type into consideration)

The following are in-built functions in julia used to test numbers for special values

In-built FunctionsMeaning
isequal(x, y)checks if x and y are identical
isfinite(x)checks if x is a finite number
isinf(x)checks if x is an infinite number
isnan(x)checks if x is not a number

Things To Note

  • isequal() can be used to distinguish between signed zeros (i.e. -0 and 0).

  • If you choose to define your own equality function, define a hash() method to ensure that isequal(x, y) implies hash(x) == hash(y)

The hash() function returns an unsigned integer number unique to every numerical value.

  • The default type for an integer literal depends on whether the target system has a 32-bit or 64-bit architecture.

  • The Julia internal variable Sys.WORD_SIZE indicates whether the target system is 32-bit or 64-bit.

  • Inf is a variable that denotes infinity.

  • NaN means 'Not a Number'.

  • e can be used in place of ^ to denote exponent or power when used with numbers. E.g. 3e3 == 3000 and 3e-3 == 0.003

This brings us to the end of the Basics of Julia & Numerical Computation series. There will be more articles on julia in the coming days. Stay tuned!!!


Resources

  • Julia Academy
  • Julia Docs
  • Curiosity