Sunday, June 25, 2017

Julia - Language - Writing and Reading files

Julia has an open function using which you can write to file and read from file. Here, we will first create a file named "myfile.txt" and then read from it. 

Step 1: Writing to a file:

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> fopen = open("myfile.txt","w")
IOStream(<file write>)

julia> write(fopen,"Hello World! Welcome to Julia"*"\n")
30

julia> close(fopen)


Step 2: Reading from files:

julia> ### reading file now

julia> fopen = open("myfile.txt","r")
IOStream(<file write>)

julia> fread = readlines(fopen)
1-element Array{String,1}:
 "Hello World! Welcome to Julia"

julia> close(fopen)

julia> fopen = open("myfile.txt","r")
IOStream(<file write>)

julia> fread = readstring(fopen)
"Hello World! Welcome to Julia\n"

julia> close(fopen)

Step 3: Let us now check for the presence of the file. We execute OS commands from within Julia:

shell> ls -l
total 8
-rw-r--r-- 1 ubuntu ubuntu   30 Jun 25 20:30 myfile.txt

julia> 

We have seen now that, writing of file has been successful. Also, we are able to read from it. 

The following are the read and write modes available in Julia Language:


Mode Description
r read
r+ read, write
w write, create, truncate
w+ read, write, create, truncate
a write, create, append
a+ read, write, create, append



Saturday, June 24, 2017

Ubuntu - Install Application - Beyond Compare



Beyond Compare is a file comparing Utility. To Install it in Linux Mint / Ubuntu:

Step 1: Download Beyond Compare from website:

$ wget http://www.scootersoftware.com/bcompare-3.3.13.18981_i386.deb
--2017-06-24 06:59:47--  http://www.scootersoftware.com/bcompare-3.3.13.18981_i386.deb
Resolving www.scootersoftware.com (www.scootersoftware.com)... 72.32.90.251
Connecting to www.scootersoftware.com (www.scootersoftware.com)|72.32.90.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10447378 (10.0M) [application/octet-stream]
Saving to: ‘bcompare-3.3.13.18981_i386.deb’

100%[==============================================================================================================================>] 1,04,47,378 1.96MB/s   in 13s    

2017-06-24 07:00:04 (766 KB/s) - ‘bcompare-3.3.13.18981_i386.deb’ saved [10447378/10447378]

/opt/softwares $ 


Step 2: Install it using dpkg:

$ sudo dpkg -i bcompare-3.3.13.18981_i386.deb 
Selecting previously unselected package bcompare.
(Reading database ... 166740 files and directories currently installed.)
Preparing to unpack bcompare-3.3.13.18981_i386.deb ...
Unpacking bcompare (3.3.13-18981) ...
Setting up bcompare (3.3.13-18981) ...
OK
Processing triggers for shared-mime-info (1.2-0ubuntu3) ...
Processing triggers for gnome-menus (3.10.1-0ubuntu2) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu1) ...
Processing triggers for mime-support (3.54ubuntu1.1) ...
 /opt/softwares $ 

Step 3: Check for installation:

/opt/softwares $ which bcompare
/usr/bin/bcompare
 /opt/softwares $ 

Step 4: You can now launch it, either by searching it in the menu. Or, you can launch it from terminal by typing - bcompare at the prompt:

/opt/softwares $ bcompare
/opt/softwares $ 

Beyond Compare is now installed in Linux Mint / Ubuntu.

Friday, June 23, 2017

Julia - Command Line - Executing Shell commands at the prompt

In Julia Language, keying in semi-colon (;) at the prompt (julia>) will change the prompt to (shell>) in which we can execute shell commands.


$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> 

shell> 

julia>


To return to the julia prompt (julia>) just key in backspace. Of course, pressing the return key will take you the prompt in the next line.

Julia - Language - Installation in Ubuntu

Visit JuliaLang website and download the current stable release: https://julialang.org/downloads/


Step 1: Open Terminal and download Julia:

$ wget https://julialang-s3.julialang.org/bin/linux/x64/0.6/julia-0.6.0-linux-x86_64.tar.gz

Step 2: Uncompress the Archive:

$ tar -zxvf julia-0.6.0-linux-x86_64.tar.gz

Step 3: After uncompressing the archive - the following folder was created:

$ ls -l
total 1686492
-rw-rw-r-- 1 ubuntu ubuntu   66543012 Jun 20 00:25 julia-0.6.0-linux-x86_64.tar.gz
drwxr-xr-x 7 ubuntu ubuntu       4096 Jun 19 19:28 julia-903644385b
$

Step 4: Move this folder to a convinient location:

$ cd /opt
/opt$

/opt$ mv /home/mhc/Downloads/julia-903644385b .
/opt$

Step 5: Create a softlink for Julia in /usr/bin - this is so that we can run it from any location:

$ cd /usr/bin
/usr/bin$ sudo ln -s /opt/julia-903644385b/bin/julia julia
/usr/bin$

Step 6: Run julia:

$ cd ~
~$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> 


Monday, June 19, 2017

Julia - Programming Language - ans variable

In an interactive session of Julia programming language. When you type an expression at the prompt, say, 1 + 5 - the output is displayed at the prompt.

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.11 (2015-07-27 06:18 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org release
|__/                   |  x86_64-linux-gnu

julia> 1+5

6

We can access this output using the variable "ans". This is because the result gets stored in the "ans" variable. 

julia> ans
6

Now, consider the below expression, in which we are explicitly storing the result in a variable, "var":

julia> var = 2 + 3
5

julia> var
5

julia> ans
5

julia>

In such a case, the answer of the expression is stored in both "var" and the "ans" variables. 

Caveat:

The binding of the result to the "ans" variable happens only during interactive sessions. It does not happen in other ways.