Monthly Archives: June 2012

Basic Matrix operation in Lazarus/FreePascal

Well.. This is my first post about FreePascal (FPC)/Lazarus. I’ve been asked to create a simulation for artificial neural-network using backpropagation without using Matlab. So, I decided to use FreePascal/Lazarus to create it. Why? Because it’s challenging.. 😀

First problem that I need to overcome is how to mimic matrix arithmetic in FreePascal just like in Matlab. So, after some searching, I found satisfactory library called DynMatrix.

Download file from here:

Extract two files inside the archive, you’ll need only: dynmatrixutils.pas and dynmatrix.pas.

Use these two files in your project/form, example:

  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
  StdCtrls, Spin, math, dynmatrix, dynmatrixutils;

Now, for the example usage:
Declare a matrix variables:

  A, B: TDMatrix;

Init a matrix into all zeros with 4 rows and 3 cols:

A:=Mzeros(4, 3);

Setting a value for an element in matrix:

A.setv(row_number, col_number, the_value);  //the value is a "double" type

Getting the value of an element:

the_value:=A.getv(row_number, col_number);

Matrix addition, subtraction, multiplication and matrix power:

C:=A+1;  //add with a constant
C:=A+B;  //add with a matrix
C:=A-1;  //substraction with constant
C:=A-B;  //substract with matrix
C:=3.4 * A; //multiply with a constant
C:=A * (-4.2);  //another form of multiplication
C:=A * B;  //multiply with another matrix
C:=A**4;  //matrix power with 4
C:=A**(-1);  //can also be powered with negative number

Matrix transpose, inverse and convolution

B:=A.t;   //transpose
B:=Minv(A);  //inverse matrix
C:=Mconv(A, B); //convolution

Converting string to matrix:

A:=StringToMatrix('1 2 3; 4 5 6');  //just like Matlab, convenient isn't it?

Creating “eye” matrix:

A:=Meye(4);  //4x4 "eye" matrix

Does this library also works on Delphi? Although I have Delphi, I haven’t tried it. So, no idea, but I’ve been looking at the source code and it seems it is OK to use it on Delphi Pascal. The dependecies is only on SysUtils, math and Classes

Free and OpenSource tabbed FileManager for Windows

There’s one that I like most: Double Commander. It’s totally free, no shareware or demoware, and open source and it’s created using Lazarus+FreePascal! Awesome!

double commander interface

double commander default two-windows interface

If you looking for an alternative to a shareware Total Commander, you are choosing the right one. Double Commander can also install+load plugin from Total Commander.

Although I said in the title it’s “for Windows” but Double Commander can run on Linux too. Since it’s created in FreePascal, it should be able to be compiled on Mac also. However the only available binary is only for Windows (32 and 64 bit) and Linux (also 32 and 64 bit).

If you don’t want to bloat your Windows registry, Double Commander also come in “portable” version. No need to install, just extract and you are good to run it.

Some notable features:

  • tabbed interface
double commander tabbed file manager

double window + tabbed file manager

  • built-in viewer (included hexviewer) and editor with syntax highlighting (although the recognized programming language is limited)
double commander built-in editor

built-in editor highlighting Visual Basic Code

  • extended search function, almost has all feature of Agent Ransack. You can easily search files that contain some words.
double commander extended search

full text search

  • built-in file comparator based on it’s content, multi-rename file, compress and decompress (ZIP, TAR.GZ, TGZ, LZMA, BZ2, RPM, CPIO, DEB, RAR), calculate (and verify) file hash, split and join files (no need hjsplit now 🙂 )
double commander additional functions

more built-in functions

If you want to download more recent (bleeding-edge/alpha) Double Commander for Windows 32bit and 64bit, you can head to:

Creating webservice server and client using NuSOAP

Brief description of NuSOAP:

NuSOAP is a rewrite of SOAPx4, provided by NuSphere and Dietrich Ayala. It is a set of PHP classes – no PHP extensions required – that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1.

Why using NuSOAP? PHP5 already has soapServer and soapClient implementation

  • it’s easy to implement and fast, no need to create WSDL document by yourself (this is my main reason why I choose NuSOAP)
  • compatibility, no special PHP extension need (I didn’t test it if still works on PHP4, but it does work flawlessly on PHP 5.0)

First, download NuSOAP from here:

The Server

We will create a SOAP server with two entry points (=function). One function take one parameter and output a string while the other one take two parameters and output a complex result (an array/struct). The codes with commentary:


$server = new nusoap_server;

$server->configureWSDL('server', 'urn:server');

$server->wsdl->schemaTargetNamespace = 'urn:server';

//SOAP complex type return type (an array/struct)
        'id_user' => array('name' => 'id_user', 'type' => 'xsd:int'),
        'fullname' => array('name' => 'fullname', 'type' => 'xsd:string'),
        'email' => array('name' => 'email', 'type' => 'xsd:string'),
        'level' => array('name' => 'level', 'type' => 'xsd:int')

//first simple function
			array('username' => 'xsd:string'),  //parameter
			array('return' => 'xsd:string'),  //output
			'urn:server',   //namespace
			'urn:server#helloServer',  //soapaction
			'rpc', // style
			'encoded', // use
			'Just say hello');  //description

//this is the second webservice entry point/function 
			array('username' => 'xsd:string', 'password'=>'xsd:string'),  //parameters
			array('return' => 'tns:Person'),  //output
			'urn:server',   //namespace
			'urn:server#loginServer',  //soapaction
			'rpc', // style
			'encoded', // use
			'Check user login');  //description

//first function implementation
function hello($username) {
        return 'Howdy, '.$username.'!';

//second function implementation 
function login($username, $password) {
        //should do some database query here
        // .... ..... ..... .....
        //just some dummy result
        return array(
		'fullname'=>'John Reese',



Suppose that you save the codes in ws.php, you can see your neat webservice description by accessing the URL:
Try click on one of your webservice entry point! NuSOAP will show you a description about it, see screenshot:

webservice entry point description using NuSOAP

example of entry point description generated by NuSOAP

for WSDL XML document, you can see it at:

The client

The code is quite simple, all the code need is your webservice WSDL URL.


//This is your webservice server WSDL URL address
$wsdl = "http://localhost/nusoap_test/ws.php?wsdl";

//create client object
$client = new nusoap_client($wsdl, 'wsdl');

$err = $client->getError();
if ($err) {
	// Display the error
	echo '

Constructor error

' . $err; // At this point, you know the call that follows will fail exit(); } //calling our first simple entry point $result1=$client->call('hello', array('username'=>'achmad')); print_r($result1); //call second function which return complex type $result2 = $client->call('login', array('username'=>'john', 'password'=>'doe') ); //$result2 would be an array/struct print_r($result2);

That’s it! Creating webservice server and client using NuSOAP is fast and simple. 🙂

Easily integrate/load PHPExcel into CodeIgniter Framework

PHPExcel is a set of PHP classes/library to read and write Excel files (.XLS/.XLSX). Based on CodeIgniter wiki page, integrating and using PHPExcel into CodeIgniter framework need some modification in PHPExcel source code. With the steps that I will show you, you will not need to edit any PHPExcel source code and the usage is very convenient just like you use on “plain-straight” PHP.

Pre-requisite: I tested this on CodeIgniter 2.1 and PHPExcel 1.7.7

Here we go:

  • download PHPExcel from (yup, they move from codeplex to github)
  • Extract the compressed archive (.zip or .tar.gz). Only extract the files inside Classes folder. Extract them to CodeIgniter’s application/third_party/ folder. Your application/third_party/ folder will look like this:
extract phpexcel into codeigniter

CI application folder’s structure

  • Create new PHP file inside CI’s application/libraries/ name it Excel.php. This is the source code (source [Bahasa Indonesia] [DEAD LINK!!]:

  • Your CI's application/libraries/ folder will looke like this:
  • codeigniter's application/libraries stuctureThat's it. Now you can use PHPExcel library in your CodeIgniter's controller code.

Example usage (with commentary):

//load our new PHPExcel library
//activate worksheet number 1
//name the worksheet
$this->excel->getActiveSheet()->setTitle('test worksheet');
//set cell A1 content with some text
$this->excel->getActiveSheet()->setCellValue('A1', 'This is just some text value');
//change the font size
//make the font become bold
//merge cell A1 until D1
//set aligment to center for that merged cell (A1 to D1)

$filename='just_some_random_name.xls'; //save our workbook as this file name
header('Content-Type: application/'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
//force user to download the Excel file without writing it to server's HD

Have fun with CodeIgniter and PHPExcel! 🙂

TREQ A10C review

TREQ A10C is a low-end Android made-in-China tablet. I bought this tablet because although it’s low-end, it runs on Android ICS 4.0.3 (and my kids want Angry Birds Space  and Plants vs Zombie in this device 😛 ). The price is also affordable, IDR1,350,000.00 (about USD150) for the 16GB version.
Some basic system info (taken using Quadrant standard):

Treq A10C system information

TREQ A10C basic info

Graphic capability (also from Quadrant):

Treq A10C graphic component

TREQ A10C graphic capability

– cheap
– run Android ICS 4.0.3
– 1GHz single core processor (tweakable up to 1.5GHz) with 1GB RAM
– able to play MKV video (tested on 720p quality), subtitle is flawlessly shown.
– able to read external hard-disk (tested on my Toshiba 500GB), be aware though, Android will only read your first hard-disk partition
– already rooted
– Google Play/Market, Adobe Flash 11, HD games run smoothly
– HDMI output (cable is not included)
– 5 points multi-touch

– no GPS
– no compass
– no built-in modem
– no bluetooth
– no camera (well, there is a front VGA camera, it sucks)

Although this tablet hasn’t any internal modem (WiFi only), it does support some USB modem (GSM and CDMA):

treq a10c supported modem

Supported USB Modem by Treq A10C