## Monday, November 29, 2010

## Thursday, November 25, 2010

### Simple Matrix Operations for the C Language

**matrix**is a simple C library for basic matrix operations. Supported operations are:

- Creating matrices
- Summation
- Subtraction
- Multiplication
- Multiplication with a scaler
- Inverse
- Determinant
- Echelon Form
- Submatrix extraction
- Saving and loading matrices

This is an open source project under the GPL. That means you can use and change it for any purpose but you have to make the source codes public.

The header file and an example are given below. Also you can download the source code by clicking here.

An example is given below:

/*================================================================matrix, a simple library for matrix operations.Copyright (C) 2010-2011 by Mehmet Hakan Satman.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License version 2as published by the Free Software Foundation.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301

The main page of this library is http://www.mhsatman.com/USAThe author may be reached atmhsatman@yahoo.com.*============================================================*/#include<stdio.h>#include<stdlib.h>#include"matrix.h" intmain() { int n=10; int m=10;/*Creating a matrix with rows n, columns m*/Matrix *m1=Matrix_create(n,m); Matrix *m2;/*Randomizing the matrix.*/int i,j; double val=1;for(i=0;i<n;i++){for(j=0;j<m;j++){ val=((double)random()/RAND_MAX)*1;if(((double)random()/RAND_MAX)<0.5) val*=-1;Matrix_set(m1,i,j,val); } }/*Dumping the content of the matrix*/Matrix_dump(m1);/*Getting inverse of the matrix.*/m1=Matrix_inverse(m1);/*Getting the second column of the matrix*/m2=Matrix_getcol(m1,1);/*Saving matrices*/Matrix_save(m1,"matrix1.dat");Matrix_save(m2,"matrix2.dat");/*Reloading matrices*/m1=Matrix_load("matrix1.dat"); m2=Matrix_load("matrix2.dat");/*Calculating determinant*/printf("Determinant of m1 is %f\n",Matrix_determinant(m1));//this will return nan, becase m2 is not a square matrixprintf("Determinant of m2 is %f\n",Matrix_determinant(m2));/*Free the memory*/Matrix_delete(m1);Matrix_delete(m2);printf("OK\n");return(0); }

## Monday, November 22, 2010

### Download Dos 6.22

Old Dos 6.22 is free for download at the Microsoft Web Site...

Yes! Microsoft must pay us for using this :)

Download Dos 6.22 For Free

Yes! Microsoft must pay us for using this :)

Download Dos 6.22 For Free

## Thursday, November 11, 2010

### Calling R from Java - RCaller

## Edit: This version of RCaller is deprecated, please check the new version of 2.0. This entry is about older versions of RCaller and may be outdated. A blog entry for version 2.0 is in http://stdioe.blogspot.com/2011/07/rcaller-20-calling-r-from-java.html and http://stdioe.blogspot.com.tr/2014/04/rcaller-220-is-released.html for RCaller version 2.2## New version : Rcaller 0.5.2## Note: The source page of this article is http://www.mhsatman.com/rcaller.php | ||||||||||||||

[2010/08/07] Now, Rcaller has a new version, 0.5.2, with some bug fixes and additional functionality. Some changes are done and some bugs are fixed by John Oliver. John is now second developer of the Rcaller. | ||||||||||||||

## Change Log for version 0.5.2: | ||||||||||||||

- Added a multi-threaded StreamReader class to RCaller, for stream reading both stderr & stdout to prevent read blocks.
- StreamReader will optionally echo what it receives to the parent process stdout & stderr, so you can see what is going on
- Changed RunRCode to use StreamReader
- Changed RunRCode to wait for the sub-process to complete before returning
- int[] RGetAsIntArray(String name) function was added so results from R functions can be handled as integer arrays
- String[] RGetAsStringArray(String name) function was added in order to handle R results as String arrays
- Removed extra cat(javaCode) call from makejava.r
| ||||||||||||||

## RCaller | ||||||||||||||

RCaller is an other simple way to call R from Java without JNI. There are lots of queries in the internet about "how to call r from java" or "call r function from java with / without JNI". There are some solutions about these works, for example, RServe is a server application written in C and it waits for socket connections, then accepts clients and runs the R code that sent from socket streams and returns SEXP 's (S / R Expressions). Also, rJava is a JNI solution for calling R from Java. But as i see, users don't want to struggle this things and they seeks more practical solutions. RCaller uses neither sockets nor JNI interface for calling R functions from Java. RCaller simply runs RScript executable file using java's Runtime and Process classes. Then runs R commands using arguments and handles results using streams. RCaller converts R objects to Java's double or String arrays using a R script and BeanShell interpreter. After these operations R results can be handled by user using getter methods. You can use it in your Java applications that needs some statistical calculations. Implementation and setting-up processes are easy. You can download source codes as Netbeans project and jars. Simply add two jars to your classpath and start calling R! | ||||||||||||||

## Examples | ||||||||||||||

## 1)Getting Pi from R!In this example, we are calling R code "a<-pi;" that sets the value of pi to variable a. Then, we handle this result from Java.
The result is 3.14159. RCaller always handles results as arrays, so a is not variable but double array. Array has only one element, so a[0] is the value that sent from R. We have to use cat(makejava(a)) to make R object 'a' usable in Java.We call RunRCode() function with 3 parameters. Last 2 parameters are boolean. If first one is true, then content of stderr will be written on console. If the second one is true, then content of stdout will be written. We set them false not to write both outputs on the screen. | ||||||||||||||

## 2)Calculate Linear Regression from Java using RIn this example, we set x and y with random variables that come from standard normal distributions and estimate linear regression using R and Java.
The result is ```
-0.815634476060036
0.637334790434423
```
so, these are the estimated coefficients of the ordinary least squres regression. | ||||||||||||||

## 3)Running RCaller in different platforms (Linux, Windows, Mac, etc)RCaller is pure Java and can be run any platform that Java virtual machine runs. Also, you need to be have R as well. Default R engine is Rscript executable file that distrubited in R. Default value of engine is /usr/bin/Rscript but user can change location using setRScriptExecutableFile(String location) method.
| ||||||||||||||

## 4)What objects returned after running my R command?RCaller converts R objects to Java objects. You can handle returned values' names like this:
The result is: ```
double[] residuals
double[] coefficients
double[] sigma
double[] df
double[] rsquared
double[] adjrsquared
double[] fstatistic
double[] covunscaled
double[] residuals
double[] coefficients
double[] sigma
double[] df
double[] rsquared
double[] adjrsquared
double[] fstatistic
double[] covunscaled
```
and these are all returned fields from the summary() R command. | ||||||||||||||

## Download source code and jars
| ||||||||||||||

If you like this solution or you have got any questions, you can send e-mail using mhsatman [at] yahoo.com. Mehmet Hakan Satman, Istanbul University, Faculty of Economics, Department of Econometrics |

Labels
r

Subscribe to:
Posts (Atom)