#include "NativeLinAlg.h"
#include "/System/Library/Frameworks/vecLib.framework/Headers/vecLib.h"



JNIEXPORT jint JNICALL Java_NativeLinAlg_dsyev(
	JNIEnv *		env, 
	jclass			obj,
	jchar			jobz_j,
	jchar			uplo_j, 
	jint			n_j,
	jdoubleArray	a_j,
	jint			lda_j,
	jdoubleArray	w_j,	
	jdoubleArray	work_j,
	jint			lwork_j,
	jintArray		info_j)
{

/* Subroutine  int dsyev_(char *jobz, char *uplo, integer *n, doublereal *a,
	 integer *lda, doublereal *w, doublereal *work, integer *lwork, 
	integer *info)
 */
	
	char		jobz = jobz_j;
	char		uplo = uplo_j;	
	__CLPK_integer			n = n_j;
	__CLPK_doublereal*		a_p = (*env)->GetDoubleArrayElements(env, a_j, 0);
	__CLPK_integer			lda = lda_j;	
	__CLPK_doublereal*		w_p = (*env)->GetDoubleArrayElements(env, w_j, 0);
	__CLPK_doublereal*		work_p = (*env)->GetDoubleArrayElements(env, work_j, 0);
	__CLPK_integer			lwork = lwork_j;		
	__CLPK_integer*			info_p = (*env)->GetIntArrayElements(env, info_j, 0);
	
	dsyev_(&jobz, &uplo, &n, a_p, &lda, w_p, 
	work_p, &lwork, 
	info_p);
	
 	(*env)-> ReleaseDoubleArrayElements(env, a_j, (double *)a_p, 0);
	(*env)-> ReleaseDoubleArrayElements(env, w_j, (double *)w_p, 0);	
	(*env)-> ReleaseDoubleArrayElements(env, work_j, (double *)work_p, 0);	
	(*env)-> ReleaseIntArrayElements(env, info_j, (jint *)info_p, 0);
	
	return info_p[0];

}

JNIEXPORT jint JNICALL Java_NativeLinAlg_dsteqr(
	JNIEnv *		env, 
	jclass			obj,
	jchar			compz_j,
	jint			n_j,
	jdoubleArray	d_j,
	jdoubleArray	e_j,
	jdoubleArray	z_j,
	jint			ldz_j,
	jdoubleArray	work_j,
	jintArray		info_j)
{

/* Subroutine  int dsteqr_(char *compz, integer *n, doublereal *d__, 
	doublereal *e, doublereal *z__, integer *ldz, doublereal *work, 
	integer *info)
*/
	char		compz = compz_j;
	__CLPK_integer			n = n_j;
	__CLPK_doublereal*		d_p = (*env)->GetDoubleArrayElements(env, d_j, 0);
	__CLPK_doublereal*		e_p = (*env)->GetDoubleArrayElements(env, e_j, 0);
	__CLPK_doublereal*		z_p = (*env)->GetDoubleArrayElements(env, z_j, 0);
	__CLPK_integer			ldz = ldz_j;
	__CLPK_doublereal*		work_p = (*env)->GetDoubleArrayElements(env, work_j, 0);
	__CLPK_integer*			info_p = (*env)->GetIntArrayElements(env, info_j, 0);
	
	dsteqr_(&compz, &n, d_p, e_p, z_p, &ldz, work_p, info_p);
 	(*env)-> ReleaseDoubleArrayElements(env, d_j, (double *)d_p, 0); 	
	(*env)-> ReleaseDoubleArrayElements(env, e_j, (double *)e_p, 0);
	(*env)-> ReleaseDoubleArrayElements(env, z_j, (double *)z_p, 0);
	(*env)-> ReleaseDoubleArrayElements(env, work_j, (double *)work_p, 0);	
	(*env)-> ReleaseIntArrayElements(env, info_j, (jint *)info_p, 0);
	
	return info_p[0];

}
