# Monthly Archives: September 2014

## Using linkbucks API / posting data using cURL in PHP

Background:

Solution:

• Register to linkbucks if you don’t have any account there (it’s free and you probably should use email other than gmail and hotmail if you want quick confirmation from linkbucks)

This is our little PHP function that help us to POST data to certain URL:

How to use it to call linkbucks API:

The curl_post function is quite generic, you can use it to send data to remote URL using POST method like this:

You should notice that the data array need to be converted using http_build_query first (in linkbucks’ case, it doesn’t need to be converted first).
You can also add more advanced feature in the function such as handling the timeout and cookie.

## Quite fast library for computing power of matrix using php

Still about Matrix, since lately I handle a lot of web-based projects that involving Matrix computations, maybe someday you’ll need this.
I’m in need of this kind of library when creating web-based ANP (Analytic Network Process) problem solver where you have to power the weighted supermatrix with some high integer number to gain the limit supermatrix.

Here’s the codes inside the library, you can save it to whatever php file:
http://pastebin.com/RmUXpBiT

(I’m sorry I don’t post the full source code on this post, WordPress always messed up the code because it contains ‘<‘ and ‘>’)

How to use it?
As simple as this:

Comparison to MATLAB
We’ll compare MATLAB result and our library result for the correctness and calculation speed.
The matrix will be taken from a CSV file and will be 15×15 in size. We will power this matrix to 512.
This is the MALTAB code used:

This is the partial screenshot of the MATLAB’s code result

power of matrix using MATLAB result

And finally our PHP code:
You can see it here: http://pastebin.com/x3hFi6tm
I won’t take the result screenshot as you can try it on http://demo.ahowto.net/matrix_power.php

If you run the MATLAB code and the PHP code, you’ll see that our PHP code yield the same result as the MATLAB code but it run much faster than the MATLAB (both already timed in miliseconds)! I absolutely don’t know why powering matrix using PHP is faster than using MATLAB but I’m happy with the result. 😀

## [Example] Solving math linear equations with 4 unknown variables

We’ll put some use of Matrix library for CodeIgniter that we already implemented to solve a linear equation.
Let’s say we have linear equations with 4 unknown variables like this:
$\begin{array}{rcl} 2a+3b+c+6d &=& 4 \\ 4a-2b+0.5c+d &=&2 \\ 6a+b-d&=&-1 \\ 2a+2b-c+3d&=&4 \end{array}$

That linear equation can be written in Matrix notation like this:
$\left[ \begin{matrix} 2&3&1&6\\ 4&-2&0.5&1 \\ 6&1&0&-1\\ 2&2&-1&3 \end{matrix} \right] \left[ \begin{matrix} a\\b\\c\\d \end{matrix} \right] = \left[ \begin{matrix} 4\\2\\-1\\4 \end{matrix} \right]$

And can be solved by this matrix equation:
$\left[ \begin{matrix} a\\b\\c\\d \end{matrix} \right] = \left[ \begin{matrix} 2&3&1&6\\ 4&-2&0.5&1 \\ 6&1&0&-1\\ 2&2&-1&3 \end{matrix} \right]^{-1} \left[ \begin{matrix} 4\\2\\-1\\4 \end{matrix} \right]$

And this is how we solve it on php/CodeIgniter:

Quite simple isn’t it?!

PS.: I’m glad I can use my Latex knowledge in this post 😛

## Matrix operation library for CodeIgniter

This tips is useful if you someday handling web-based project that involving Matrix handling and you want to implement it using CodeIgniter. We’ll use PHPMath library for this.

• Open the archive (build02.tar.gz) and see the content, you’ll see “build02” folder, go inside, you’ll see more folders there:

phpmath archive content

• Because we only need the matrix handling and operation library, we just need the “JAMA” folder inside.
• Extract this “JAMA” folder into your CodeIgniter’s application/third_party/ library, rename it to more meaningful folder, I prefer “phpmath”. Your third_party library will be look like this:

phpmath inside third_party folder

• Go inside “phpmath” folder, you’ll see more folders there. We don’t need “docs”, “examples” and “tests” folder, we can delete them, your phpmath folder will be like this:

phpmath after deleting unnecessary folders

And you are done! (In this tips: not like mPDF and PHPExcel library, we don’t need to create a new file/class in the application/libraries folder)

How to use it?

Quite simple, check and test these code samples inside your controller:

## Play DASH video and DASH audio at the same time using VLC

DASH video and DASH audio formats option in vrank.org

Why? Well, the simple answer is: google/youtube separate the video into two stream: video and audio. So, when you are streaming a youtube video (using browser) in high quality format (720p and up), you actually download/stream from two different files.

Now, how can you play these two files (DASH video and DASH audio) at the same time? We will use VLC to do this!

• Open VLC
• Access Menu: Media –> Open Multiple Files…

VLC: Media –> Open Multiple Files…

• A new empty “Open Media” window will be opened, click on that “Add” button then browse and select your first file (could be DASH video or DASH audio file, not in particular order)

VLC: Open Media window

• Check/click/activate “Play another media synchronously …” then click on “Browse…” button

VLC: play other media synchronously

• A new empty “Open Media” window will be opened. Click “Add“, browse to your second files (either DASH audio or DASH video depended on your first opened file). Then click “Select” button (the button will become active/clickable after you choose your second file), this second “Open Media” window will be automatically closed.

VLS: a new empty “Open Media” window

• Now, you’ll have the two files chosen and listed on the first “Open Media” window. You may click “Play” button.

VLC: two files is selected

VLC will play those two files synchronously, you may check it via VLC menu: Tools –> Codec Information, there will be two stream played together.

VLC playing DASH video and DASH audio at the same time