Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanhezhao committed Nov 24, 2018
1 parent 91f0bd3 commit 6b95410
Show file tree
Hide file tree
Showing 28 changed files with 949 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
55 changes: 55 additions & 0 deletions CRT_sum_mex_matrix_v1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

#include <mex.h>
#define MAX(a,b) ((a) > (b) ? a : b)
/* L = CRT_sum_mex_matrix(X,r);
X is a K*N sparse matrix, r is a 1*N vector, L is a 1*N vector */


void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
mwSize Lenx;
mwIndex i, j, ij, k;
double *ZSDS, *RND, *Lsum, *prob;
double maxx, *r_i;

double *pr;
mwIndex *ir, *jc;
mwIndex Vsize, Nsize, Ksize;
mwIndex starting_row_index, stopping_row_index, current_row_index;


pr = mxGetPr(prhs[0]);
ir = mxGetIr(prhs[0]);
jc = mxGetJc(prhs[0]);
Ksize = mxGetM(prhs[0]);
Nsize = mxGetN(prhs[0]);



r_i = mxGetPr(prhs[1]);

plhs[0] = mxCreateDoubleMatrix(1, Nsize, mxREAL);
Lsum = mxGetPr(plhs[0]);

for (j=0;j<Nsize;j++) {
starting_row_index = jc[j];
stopping_row_index = jc[j+1];
if (starting_row_index == stopping_row_index)
continue;
else {
for (current_row_index = starting_row_index; current_row_index<stopping_row_index; current_row_index++) {
maxx = MAX(maxx,pr[current_row_index]);
}
prob = (double *) mxCalloc(maxx, sizeof(double));
for(i=0;i<maxx;i++)
prob[i] = r_i[j]/(r_i[j]+i);

for (Lsum[j]=0, current_row_index = starting_row_index; current_row_index<stopping_row_index; current_row_index++)
/*k = ir[current_row_index];*/
for(i=0;i<pr[current_row_index];i++) {
if ((double) random() <= prob[i]*RAND_MAX)
Lsum[j]++;
}
}
}
}
Binary file added CRT_sum_mex_matrix_v1.mexa64
Binary file not shown.
Binary file added CRT_sum_mex_matrix_v1.mexmaci64
Binary file not shown.
35 changes: 35 additions & 0 deletions CRT_sum_mex_v1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#include <mex.h>
/*L = CRT_sum_mex(x,r);*/


void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
mwSize Lenx;
mwIndex i, j, ij;
double *x, *RND, *Lsum, *prob;
double maxx, r;
Lenx = mxGetM(prhs[0])*mxGetN(prhs[0]);
x = mxGetPr(prhs[0]);
r = mxGetScalar(prhs[1]);


plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
Lsum = mxGetPr(plhs[0]);


for(i=0, maxx=0;i<Lenx;i++)
if (maxx<x[i]) maxx = x[i];

prob = (double *) mxCalloc(maxx, sizeof(double));

for(i=0;i<maxx;i++)
prob[i] = r/(r+i);

for(ij=0, i=0, Lsum[0]=0;i<Lenx;i++)
for(j=0;j<x[i];j++) {
/*if ( ((double) randomMT() / (double) 4294967296.0) <prob[j]) Lsum[0]++;*/
/*if ((double) randomMT() <= prob[j]*RAND_MAX_32) Lsum[0]++;*/
if ( (double) rand() <= prob[j]*RAND_MAX) Lsum[0]++;
}
}
Binary file added CRT_sum_mex_v1.mexa64
Binary file not shown.
Binary file added CRT_sum_mex_v1.mexmaci64
Binary file not shown.
68 changes: 68 additions & 0 deletions Mult_Sparse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*==========================================================
* Mult_Sparse.c -
*
*
* The calling syntax is:
*
* Xout = Mult_Sparse(Xmask,Phi,Theta);
* Xmask is a sparse matrix
* Phi and Theta are full matrices
*
* This is a MEX-file for MATLAB.
* Copyright 2015 Mingyuan Zhou
*
*========================================================*/
/* $Revision: 0.1 $ */

#include "mex.h"

void Mult_Matrix(double *Phi, double *Theta, mwIndex *ir, mwIndex *jc, double *pr, mwSize Vsize, mwSize Nsize, mwSize Ksize)
{

mwIndex k, j, v, token, total=0;
mwIndex starting_row_index, stopping_row_index, current_row_index;
double cum_sum;

for (j=0;j<Nsize;j++) {
starting_row_index = jc[j];
stopping_row_index = jc[j+1];
if (starting_row_index == stopping_row_index)
continue;
else {
for (current_row_index = starting_row_index; current_row_index<stopping_row_index; current_row_index++) {
v = ir[current_row_index];
for (cum_sum=0,k=0; k<Ksize; k++) {
/*//prob[k] = Phi(v+ k*Vsize)*Theta(k + Ksize*i);*/
cum_sum += Phi[v+ k*Vsize]*Theta[k + Ksize*j];
}
pr[total++] = cum_sum;
}
}
}
}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *Phi, *Theta;
double *pr;
mwIndex *ir, *jc;
mwIndex Vsize, Nsize, Ksize;


Vsize = mxGetM(prhs[0]);
Nsize = mxGetN(prhs[0]);
Ksize = mxGetN(prhs[1]);
Phi = mxGetPr(prhs[1]);
Theta = mxGetPr(prhs[2]);


plhs[0] = mxDuplicateArray(prhs[0]);
pr = mxGetPr(plhs[0]);
ir = mxGetIr(plhs[0]);
jc = mxGetJc(plhs[0]);


Mult_Matrix(Phi, Theta, ir, jc, pr, Vsize, Nsize, Ksize);
}
Binary file added Mult_Sparse.mexa64
Binary file not shown.
Binary file added Mult_Sparse.mexmaci64
Binary file not shown.
118 changes: 118 additions & 0 deletions Multrnd_Matrix_mex_fast_v1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*==========================================================
* Multrnd_Matrix_mex.c -
*
*
* The calling syntax is:
*
* [ZSDS,WSZS] = Multrnd_Matrix_mex_fast(Xtrain,Phi,Theta);
*
* This is a MEX-file for MATLAB.
* Copyright 2012 Mingyuan Zhou
*
* v1: replace (double) randomMT()/RAND_MAX_32 with (double) rand() / RAND_MAX
* Oct, 2015
*========================================================*/
/* $Revision: v1 $ */

#include "mex.h"
#include "string.h"
#include <math.h>
#include <stdlib.h>
/*
* #include "cokus.c"
* #define RAND_MAX_32 4294967295.0*/

mwIndex BinarySearch(double probrnd, double *prob_cumsum, mwSize Ksize) {
mwIndex k, kstart, kend;
if (probrnd <=prob_cumsum[0])
return(0);
else {
for (kstart=1, kend=Ksize-1; ; ) {
if (kstart >= kend) {
return(kend);
}
else {
k = kstart+ (kend-kstart)/2;
if (prob_cumsum[k-1]>probrnd && prob_cumsum[k]>probrnd)
kend = k-1;
else if (prob_cumsum[k-1]<probrnd && prob_cumsum[k]<probrnd)
kstart = k+1;
else
return(k);
}
}
}
return(k);
}

void Multrnd_Matrix(double *ZSDS, double *WSZS, double *Phi, double *Theta, mwIndex *ir, mwIndex *jc, double *pr, mwSize Vsize, mwSize Nsize, mwSize Ksize, double *prob_cumsum)
/*//, mxArray **lhsPtr, mxArray **rhsPtr)*/
{

double cum_sum, probrnd;
mwIndex k, j, v, token, ji=0, total=0;
/*//, ksave;*/
mwIndex starting_row_index, stopping_row_index, current_row_index;


for (j=0;j<Nsize;j++) {
starting_row_index = jc[j];
stopping_row_index = jc[j+1];
if (starting_row_index == stopping_row_index)
continue;
else {
for (current_row_index = starting_row_index; current_row_index<stopping_row_index; current_row_index++) {
v = ir[current_row_index];
for (cum_sum=0,k=0; k<Ksize; k++) {
cum_sum += Phi[v+ k*Vsize]*Theta[k + Ksize*j];
prob_cumsum[k] = cum_sum;
}
for (token=0;token< pr[total];token++) {
/*probrnd = (double) randomMT()/RAND_MAX_32*cum_sum;*/
probrnd = (double) rand() / RAND_MAX*cum_sum;

ji++;


k = BinarySearch(probrnd, prob_cumsum, Ksize);

ZSDS[k+Ksize*j]++;
WSZS[v+k*Vsize]++;
}
total++;
}
}
}

}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *ZSDS, *WSZS, *Phi, *Theta, *RND;
double *pr, *prob_cumsum;
mwIndex *ir, *jc;
mwIndex Vsize, Nsize, Ksize;


pr = mxGetPr(prhs[0]);
ir = mxGetIr(prhs[0]);
jc = mxGetJc(prhs[0]);
Vsize = mxGetM(prhs[0]);
Nsize = mxGetN(prhs[0]);
Ksize = mxGetN(prhs[1]);
Phi = mxGetPr(prhs[1]);
Theta = mxGetPr(prhs[2]);


plhs[0] = mxCreateDoubleMatrix(Ksize,Nsize,mxREAL);
plhs[1] = mxCreateDoubleMatrix(Vsize,Ksize,mxREAL);
ZSDS = mxGetPr(plhs[0]);
WSZS = mxGetPr(plhs[1]);

prob_cumsum = (double *) mxCalloc(Ksize,sizeof(double));

Multrnd_Matrix(ZSDS, WSZS, Phi, Theta, ir, jc, pr, Vsize, Nsize, Ksize, prob_cumsum);

}
Binary file added Multrnd_Matrix_mex_fast_v1.mexa64
Binary file not shown.
Binary file added Multrnd_Matrix_mex_fast_v1.mexmaci64
Binary file not shown.
Loading

0 comments on commit 6b95410

Please sign in to comment.