How to install Lapack on Mac OS?

This is a question I get asked a lot. Lapack is actually included in Accelerate library provided by Apple. You can include it in the header file of your C++ source code. Refer to this post for more information on how to use Lapack included in Accelerate.

If you like to use the latest version of Lapack, you can easily install it using Homebrew. Enter the following command in a terminal window to install Homebrew:

/usr/bin/ruby -e "$(curl -fsSL"

To install Lapack libraries, you should first install GFortran which is included as a part of GCC. Run the following command in a terminal window:

brew install gcc

Now, you can simply install Lapack by typing the following command in a terminal window:

brew install lapack

Please let me know your questions in the comment section.

Write a solver for systems of equations by using Lapack on Mac OS

Apple provides Accelerate library which includes linear algebra library (Lapack). In this post, we will go through writing a simple C++ program to use this library on Mac OS.

Let’s solve the following equations:

We can rewrite the equations as:

Now, let’s write our code:

#include <iostream>
#include <Accelerate/Accelerate.h>

int main()
int number_of_rows = 2;
int number_of_cols = 2;
int number_of_right_hand_side_cols = 1;
int LDA = 2;
int LDB = 2;
int IPIV[3];
int INFO=10;
char TRANS = 'N'; // Non transpose

double a[2*2] = {2, 1, 
		1, -1};
double b[2] = {0,

dgetrf_(&number_of_rows, &number_of_cols, a, &LDA, IPIV, &INFO);
if (!INFO)
	std::cout << "LU factorization executed without errors." << std::endl;

dgetrs_(&TRANS, &number_of_rows, &number_of_right_hand_side_cols, 
		a, &LDA, IPIV, b, &LDB, &INFO);

std::cout << "Result: x_0 = "<< *b <<", x_1 = "<<*(b+1) << std::endl;

if (!INFO)
	std::cout << "Solver executed without errors." << std::endl;


Now, run the code by using the following commands in a terminal:

g++ -llapack lapack_test.cpp -o ltest.o

What is my gateway address?

Choosing a gateway address on Linux machines is an easy task to perform. Let’s consider a network consisting of three Linux machines as Figure 1 shows. 

Let’s consider following subnet and IP addresses:

Figure 1. Network Setup
  • Subnet 1:, IP Address 1:, and IP address 2:
  • Subnet 2: and IP Address 3:
  • Subnet 3: , IP Address 4:, and IP address 6:
  • Subnet 4: and IP Address 4:

Run “route -n” command to check the current routes on each PC. Then use “ip route flush table main” command to clear the routing tables. Also, we need to enable IP forwarding on the routers.

Since PC 1 is only connected to subnet 1, we first add a route to subnet 1 using following command:

$ route add -net netmask gw

To connect PC 1 to other subnets on the network, we either need to add other subnets addresses to the routing table one by one or add a default gateway to send all the packets not intended for Subnet 1 to that gateway.

$ route add default gw

Similar to PC 1, we can add the routes to other PCs on the network based on the tables in Figure 1.

On PC 2:

$ route add -net netmask gw
$ route add -net netmask gw
$ route add -net netmask gw
$ route add -net netmask gw

And the default gateway is for all the packets to other networks which are not directly connected to PC 2:  

$ route add default gw

PC 3 is directly connected to Subnet 3, but it is not directly connected to Subnets 1, 2, and 4. To add the routes, we use the following commands:

$ route add -net netmask gw
$ route add -net netmask gw
$ route add -net netmask gw
$ route add -net netmask gw

Asymmetric Static Routing Network using Ubuntu Machines

In asymmetric routing, data packets take different paths to go from source to destination and to come back [source].  To set up an asymmetric network on Linux machines running Ubuntu 16.04, first, we need to configure the systems to act as routers. Let’s consider a network of 3 hosts and 3 routers as Figure 1 shows. The routers are going to be Linux systems bundled with several NICs.

Click on the image to enlarge it.

Figure 1. Network layout.

The routers should be able forward the packets from one network interface card’s (NIC) port to the others. In a terminal window, enter the following command under root privileges (only on routers):

sysctl net.ipv4.ip_forward=1

We also need to ensure that packets coming from a different path that they were sent to, are not dropped as well. Enter the following command (only on routers):

sysctl net.ipv4.conf.all.rp_filter=2

Now, we are ready to assign static routes to the machines. We consider following subnets here:

  • The subnet for Host 1 connection to Router 1:
  • The subnet for Host 2 connection to Router 2:
  • The subnet for Host 3 connection to Router 3:
  • The subnet for Router 1 connection to Router 2:
  • The subnet for Router 1 connection to Router 3:
  • The subnet for Router 2 connection to Router 3:

Figure 2 shows the assigned IP address to each port of Linux machines.

Click on the image to enlarge it.

Figure 2. Assigned IP addresses to each port.

The only remaining step is to set up routing tables on each system. To assign routes to each machine we use the “route” command in terminal.

Hosts use their own assigned IP address for the gateway to their subnets and the IP address of next hop on the same subnet for the gateway to other networks. For example, Host 1 uses the gateway for network and the gateway to connect to network  We run the following commands for these two networks on Host 1:

route add -net netmask gw
route add -net netmask gw

We need to run the same command to add all the 6 subnets to every host and router on the network. Figure 3 shows the requires routing tables for each machine.

Click on the image to enlarge it.

Figure 3. Routing tables for each system. Now we can also add alternate routes to the same network with a higher metric (lower priority) using the “route” command. For example, we could add the following backup route to Router 2:

route add -net netmask gw metric 100

We can test this route by running ping command on a specific interface of  router 2:

ping -I interface_connected_from_Router_2_to_Router_1

Let me know if you had any questions in the comment section.

Connect to MS SQL on MacOS using Python

Microsoft has released a beta version of its ODBC driver for MacOS. Here is a quick and easy guide to connect to your MS SQL using python.

The first thing you need is to install Homebrew.

Enter the following command in a terminal window to install Homebrew:

/usr/bin/ruby -e "$(curl -fsSL"

Afterward, enter following commands in a terminal window to install Microsoft ODBC driver 13 for MacOS:

brew tap microsoft/msodbcsql
brew update
brew install msodbcsql

Next item you need to install is pyodbc by entering following command in a terminal:

pip install pyodbc

or depending on your python installation:

python3 -m pip install pyodbc

Now you have all the prerequisites to connect to your MS SQL database in python.

Now navigate to unixodbc folder using the following command:

cd /usr/local/Cellar/unixodbc/2.3.4

Here you have two files:



Open odbc.ini using the following command:

nano odbcinst.ini

There you will see following information:

[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server

You need the copy the content in the square brackets which in my case is “ODBC Driver 13 for SQL Server”.

Exit the editor and open a new file like this:

nano ~/tempfile

Add the following lines to this file:

Description = Test to SQLServer
Driver = ODBC Driver 13 for SQL Server
Trace = No
Server = YourSQLServerAddress

Replace “ODBC Driver 13 for SQL Server” with the content you copied in the square brackets. Also, write your SQL server address instead of “YourSQLServerAddress“. Save the file and exit Nano editor.

Now, enter the following command in terminal and enter your password:

sudo odbcinst -i -s -f ~/tempfile -l

To test your connection, open python editor and run the following script:

import pyodbc

DSN is the name you used in the temp file. Replace yourUserName and yourPassWord with the ones you use for your SQL server.

You will see an output similar to:

<pyodbc.Connection object at 0xfffffffff>

Connect to and manage MS SQL from MacOS

In order to connect to Azure SQL Database from MacOS (OS X) for free, you need two things:

1- Oracle SQL Developer (Download)

2- JTDS driver (Download)

Download these files and install Oracle SQL Developer. Then extract the JTDS driver zip file in a desired location (e.g., \Users\”Your User Name”\JTDS\).

Now open your Oracle SQL Developer and from the menu open preference as following figure shows:

SQL Developer

SQL Developer

From the Preference window, open Database > Third Party JDBC Drivers:

Click on the photo to open the full size image!

SQL Developer

SQL Developer

Here click on Add Entry and find your JTDS folder and choose jtds-1.3.1.jar as the driver path and click on Select and then OK.

Note: Before moving on from this part, make sure you have added your IP address in the firewall setting of your Azure Portal’s desired database.

Now you are ready to add your database to the program. From the connection pane on the left, click on the “+” button.

SQL Developer

SQL Developer

Now click on “SQLServer” tab and add your database connection information:

SQL Developer

SQL Developer

Click on connect. Don’t forget to retrieve your database here. Now save this profile. Now your database is ready to be inquired by transact SQL.