Commit ce4080b3 authored by Giovanne Marcelo's avatar Giovanne Marcelo

beautify algorithm#1

parent c4fffa29
Pipeline #6397 passed with stage
in 4 minutes and 46 seconds
......@@ -9,8 +9,11 @@
#include <stddef.h>
#include <string.h>
#define double_args double, double, double, double*, double*
typedef struct {
list(long unsigned int, *data);
list(long unsigned int, *data);
}omega;
typedef struct {
......@@ -32,6 +35,6 @@ double find_minimun(matrix residual, matrix W, matrix H, double lamb,list(long u
void s(double r, double w, double h, double *numerator, double *denominator);
void ccd_pp(matrix residual, matrix W, matrix H, double lamb, int k);
void perform(algorithm_args *args);
void ccd(matrix residual, matrix W, matrix H,omega omegai, omega omegaj, double lamb, long unsigned int k,long unsigned int T);
void ccd(algorithm_args *args);
#endif
......@@ -27,18 +27,28 @@
* end for
*/
void update_col_residual(matrix residual, list(long unsigned int, omegai), double z, matrix W, matrix H, long unsigned int i, long unsigned int t) {
list_each_elem(omegai, elem) {
double result = matrix_get(residual, i, *elem) - (z - matrix_get(W, i, t) )*matrix_get(H, *elem, t);
void update_col_residual(matrix residual, list(long unsigned int, omegai),
double z, matrix W, matrix H, long unsigned int i,
long unsigned int t) {
list_each_elem(omegai, elem) {
double Rij = matrix_get(residual, i, *elem);
double Wit = matrix_get(W, i, t);
double Hjt = matrix_get(H, *elem, t);
double result = Rij - (z - Wit) * Hjt;
matrix_set(residual, i, *elem, result );
}
}
void update_row_residual(matrix residual, list(long unsigned int, omegaj), double s, matrix W, matrix H, long unsigned int j, long unsigned int t) {
list_each_elem(omegaj, elem) {
double result = matrix_get(residual, *elem, j) - (s - matrix_get(H, j, t) ) * matrix_get(W, *elem, t);
void update_row_residual(matrix residual, list(long unsigned int, omegaj),
double s, matrix W, matrix H, long unsigned int j,
long unsigned int t) {
list_each_elem(omegaj, elem) {
double Rij = matrix_get(residual, *elem, j);
double Wit = matrix_get(W, *elem, t);
double Hjt = matrix_get(H, j, t);
double result = Rij - (s - Hjt) * Wit;
matrix_set(residual, *elem, j, result );
}
}
......@@ -54,40 +64,46 @@ void s(double r, double w, double h, double *numerator, double *denominator) {
}
double find_minimun(matrix residual, matrix W, matrix H, double lamb,list(long unsigned int, om) ,long unsigned int t, long unsigned int i, void (*f)(double, double, double, double*, double*)) {
double find_minimun(matrix residual, matrix W, matrix H, double lamb,
list(long unsigned int, om) ,long unsigned int t,
long unsigned int i,void (*f)(double_args)) {
double rij, wit, hjt, numerator, denominator;
double Rij, Wit, Hjt, numerator, denominator;
numerator = denominator = .0;
list_each_elem(om,elem) {
rij = matrix_get(residual,i, *elem);
wit = matrix_get(W, i, t);
hjt = matrix_get(H, *elem, t);
(*f)(rij, wit, hjt,&numerator, &denominator);
Rij = matrix_get(residual,i, *elem);
Wit = matrix_get(W, i, t);
Hjt = matrix_get(H, *elem, t);
(*f)(Rij, Wit, Hjt,&numerator, &denominator);
}
denominator += lamb;
return ( numerator / denominator );
}
void ccd(matrix residual, matrix W, matrix H, omega omegai, omega omegaj, double lamb, long unsigned int k,long unsigned int T) {
void ccd(algorithm_args *args) {
double rating;
for(long unsigned int iter = 0; iter < T ; iter++) {
for(long unsigned int i = 0; i < residual->size1 ; i++ ) {
for(long unsigned int t = 0; t < k; t++) {
rating = find_minimun(residual, W, H, lamb, omegai.data[i], t, i, z);
update_col_residual(residual,omegai.data[i], rating, W, H, i, t ) ;
matrix_set(W, i, t, rating);
for(long unsigned int iter = 0; iter < args->T ; iter++) {
for(long unsigned int i = 0; i < args->residual->size1 ; i++ ) {
for(long unsigned int t = 0; t < args->k; t++) {
rating = find_minimun(args->residual, args->W, args->H, args->lamb, \
args->omegai.data[i], t, i, z);
update_col_residual(args->residual, args->omegai.data[i], rating, \
args->W, args->H, i, t ) ;
matrix_set(args->W, i, t, rating);
}
}
for(long unsigned int j = 0; j < residual->size2; j++) {
for(long unsigned int t = 0; t < k; t++) {
rating = find_minimun(residual, W, H, lamb, omegaj.data[j], t, j, s);
update_row_residual(residual, omegaj.data[j], rating, W, H, j, t);
matrix_set(H, j, t, rating);
for(long unsigned int j = 0; j < args->residual->size2; j++) {
for(long unsigned int t = 0; t < args->k; t++) {
rating = find_minimun(args->residual, args->W, args->H, args->lamb, \
args->omegaj.data[j], t, j, s);
update_row_residual(args->residual, args->omegaj.data[j], rating, \
args->W, args->H, j, t);
matrix_set(args->H, j, t, rating);
}
}
}
......@@ -111,6 +127,6 @@ void ccd(matrix residual, matrix W, matrix H, omega omegai, omega omegaj, double
void perform(algorithm_args *args) {
ccd(args->residual, args->W, args->H, args->omegai, args->omegaj, args->lamb, args->k, args->T);
ccd(args);
}
......@@ -55,7 +55,7 @@ PGconn* db_connect(void) {
return conn;
}
DBresult* db_select( const char *columns, const char *table, const char *where) {
DBresult* db_select( const char *columns, const char *table, const char *where){
PGconn *conn = db_connect();
char query[QUERY_LEN];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment