コード例 #1
0
int is_magic_square(Matrix *A){
    //Case that rows and columns not the same
    //Not a Square grid, so cannot be square
    if (A->rows != A->cols){
        printf("Not a square matrix\n");
        return 0;
    }

    //Getting sum of rows
    double *allRowSum = row_sum(A);
    //Get sum of columns
    double *allColSum = col_sum(A);
    //Get sum of diagnols
    double *allDiag = diag_sum(A);
    //Get total of first row - all others should be the same either way
    double magic = allRowSum[0];    

    if (test_rows(allRowSum, A->rows, magic)){
        if (test_cols(allColSum, A->cols, magic)){
            if (test_diag(allDiag, magic)){
                //If rows, cols, and diags are the same
                //We have a magic square!
                return 1;
            }
        }
    }

    return 0;
}
コード例 #2
0
int is_magic_square(Matrix *A) {
    assert((A->rows > 1) && (A->cols > 1));
    assert(A->cols == A->rows);
    
    int res = 1;
    double *row_vals = row_sum(A);
    double *col_vals = col_sum(A);

    double comp_val = row_vals[0];

    int i;
    for (i = 0; i < A->rows; i++) {
        if (comp_val != row_vals[i]) res = 0;
    }
    for (i = 0; i < A->cols; i++) {
        if (comp_val != col_vals[i]) res = 0;
    }

    double sum1 = 0;
    double sum2 = 0;
    int j;
    for (i = 0; i < A->rows; i++) {
        j = (A->rows-1) - i;
        sum1 += A->data[i][i];
        sum2 += A->data[j][i];
    }
    if ((sum1 != comp_val) || (sum2 != comp_val)) res = 0;

    return res;
}
コード例 #3
0
ファイル: matrix2.c プロジェクト: rdedhia/SoftwareSystems
// Returns 1 if is_magic_square is a magic square, otherwise returns 0
int is_magic_square(Matrix *A) {
    assert(A->rows == A->cols);
    int i;
    int n = A->rows;
    double check;
    double diag_a = 0.0;
    double diag_b = 0.0;

    double *res = row_sum(A);
    // col_res contains the sums of the columns
    double *col_res = col_sum(A);
    check = res[0];

    // loop and if statements to return 0 if all rows, columns, and 
    // diagonals do not add to the same value
    for (i=0; i<n; i++) {
        diag_a += A->data[i][i];
        diag_b += A->data[i][(n-1)-i];
        if ((res[i] != check) || (col_res[i] != check))
            return 0;
    }
    if ((diag_a != check) || (diag_b != check))
        return 0;
    return 1;
}
コード例 #4
0
ファイル: test_matrix.cpp プロジェクト: AEMPhil/Oneka
//-----------------------------------------------------------------------------
// TestMatrixColumnSum
//-----------------------------------------------------------------------------
bool TestMatrixColumnSum()
{
   Matrix A("1,2,3;4,5,6;7,8,9");
   Matrix x;
   ColumnSum( A, x );
   Matrix col_sum("12,15,18");

   return ApproxEqual(x,col_sum,TOLERANCE);
}
コード例 #5
0
   int main() {
    int i;

    Matrix *A = make_matrix(3, 4);
    consecutive_matrix(A);
    printf("A\n");
    print_matrix(A);

    Matrix *C = add_matrix_func(A, A);
    printf("A + A\n");
    print_matrix(C);

    Matrix *B = make_matrix(4, 3);
    increment_matrix(B, 1);
    printf("B\n");
    print_matrix(B);

    Matrix *D = mult_matrix_func(A, B);
    printf("D\n");
    print_matrix(D);

    double sum = matrix_sum1(A);
    printf("sum = %lf\n", sum);

    sum = matrix_sum2(A);
    printf("sum = %lf\n", sum);


    printf("A\n");
    print_matrix(A);

    double *sums = row_sum(A);
    for (i=0; i<A->rows; i++) {
       printf("row %d\t%lf\n", i, sums[i]);
   }
    // should print 6, 22, 38

    double *sums2 = col_sum(A);
    for (i=0; i<A->cols; i++) {
       printf("col %d\t%lf\n", i, sums2[i]);
   }

    Matrix *E = make_matrix(3, 3);
    E->data[0][0] = 2; E->data[0][1] = 7; E->data[0][2] = 6;
    E->data[1][0] = 9; E->data[1][1] = 5; E->data[1][2] = 1;
    E->data[2][0] = 4; E->data[2][1] = 3; E->data[2][2] = 8;
    printf("E\n");
    print_matrix(E);

    printf("Magic matrix?: %d \n", is_magic_square(E));  

   return 0;
}
コード例 #6
0
ファイル: estimate.c プロジェクト: Rygbee/ctm-c
void expectation(corpus* corpus, llna_model* model, llna_ss* ss,
                 double* avg_niter, double* total_lhood,
                 gsl_matrix* corpus_lambda, gsl_matrix* corpus_nu,
                 gsl_matrix* corpus_phi_sum,
                 short reset_var, double* converged_pct)
{
    int i;
    llna_var_param* var;
    doc doc;
    double lhood, total;
    gsl_vector lambda, nu;
    gsl_vector* phi_sum;

    *avg_niter = 0.0;
    *converged_pct = 0;
    phi_sum = gsl_vector_alloc(model->k);
    total = 0;
    for (i = 0; i < corpus->ndocs; i++)
    {
        printf("doc %5d   ", i);
        doc = corpus->docs[i];
        var = new_llna_var_param(doc.nterms, model->k);
        if (reset_var)
            init_var_unif(var, &doc, model);
        else
        {
            lambda = gsl_matrix_row(corpus_lambda, i).vector;
            nu= gsl_matrix_row(corpus_nu, i).vector;
            init_var(var, &doc, model, &lambda, &nu);
        }
        lhood = var_inference(var, &doc, model);
        update_expected_ss(var, &doc, ss);
        total += lhood;
        printf("lhood %5.5e   niter %5d\n", lhood, var->niter);
        *avg_niter += var->niter;
        *converged_pct += var->converged;
        gsl_matrix_set_row(corpus_lambda, i, var->lambda);
        gsl_matrix_set_row(corpus_nu, i, var->nu);
        col_sum(var->phi, phi_sum);
        gsl_matrix_set_row(corpus_phi_sum, i, phi_sum);
        free_llna_var_param(var);
    }
    gsl_vector_free(phi_sum);
    *avg_niter = *avg_niter / corpus->ndocs;
    *converged_pct = *converged_pct / corpus->ndocs;
    *total_lhood = total;
}
コード例 #7
0
ファイル: TopicSearch.cpp プロジェクト: pillhead/ml-lib
mat TopicSearch::init_topical_Multinomial_probabilities(){

	mat mprob = ones<mat>(this->num_topics_, this->num_topics_) * 1e-24;

	for (size_t i = 0; i < this->num_topics_; i++){
		mprob(i, i) *= 1.75;
		if (i > 0) mprob(i-1, i) *= 1.25;
		if (i < this->num_topics_ - 1) mprob(i+1, i) *= 1.25;
	}

	// normalize over columns
	rowvec col_sum = sum(mprob, 0);
	for (size_t i = 0; i < mprob.n_cols; i++)
		mprob.col(i) /= col_sum(i);

	return mprob;
}
コード例 #8
0
ファイル: estimate.c プロジェクト: Rygbee/ctm-c
void inference(char* dataset, char* model_root, char* out)
{
    int i;
    char fname[100];

    // read the data and model
    corpus * corpus = read_data(dataset);
    llna_model * model = read_llna_model(model_root);
    gsl_vector * lhood = gsl_vector_alloc(corpus->ndocs);
    gsl_matrix * corpus_nu = gsl_matrix_alloc(corpus->ndocs, model->k);
    gsl_matrix * corpus_lambda = gsl_matrix_alloc(corpus->ndocs, model->k);
    // gsl_matrix * topic_lhoods = gsl_matrix_alloc(corpus->ndocs, model->k);
    gsl_matrix * phi_sums = gsl_matrix_alloc(corpus->ndocs, model->k);

    // approximate inference
    init_temp_vectors(model->k-1); // !!! hacky
    sprintf(fname, "%s-word-assgn.dat", out);
    FILE* word_assignment_file = fopen(fname, "w");
    for (i = 0; i < corpus->ndocs; i++)
    {
        doc doc = corpus->docs[i];
        llna_var_param * var = new_llna_var_param(doc.nterms, model->k);
        init_var_unif(var, &doc, model);

        vset(lhood, i, var_inference(var, &doc, model));
        gsl_matrix_set_row(corpus_lambda, i, var->lambda);
        gsl_matrix_set_row(corpus_nu, i, var->nu);
        gsl_vector curr_row = gsl_matrix_row(phi_sums, i).vector;
        col_sum(var->phi, &curr_row);
        write_word_assignment(word_assignment_file, &doc, var->phi);

        printf("document %05d, niter = %05d\n", i, var->niter);
        free_llna_var_param(var);
    }

    // output likelihood and some variational parameters
    sprintf(fname, "%s-ctm-lhood.dat", out);
    printf_vector(fname, lhood);
    sprintf(fname, "%s-lambda.dat", out);
    printf_matrix(fname, corpus_lambda);
    sprintf(fname, "%s-nu.dat", out);
    printf_matrix(fname, corpus_nu);
    sprintf(fname, "%s-phi-sum.dat", out);
    printf_matrix(fname, phi_sums);

}
コード例 #9
0
/* 
   http://en.wikipedia.org/wiki/Magic_square

   A magic square is an arrangement of numbers (usually integers) in a
   square grid, where the numbers in each row, and in each column, and
   the numbers in the forward and backward main diagonals, all add up
   to the same number. 

   Write a function called is_magic_square() that takes a matrix and 
   returns an int, 1 if the matrix is a magic square, and 0 otherwise.

   Feel free to use row_sum().
*/
int is_magic_square(Matrix *M) {
    // adding rows should be fastest due to caching, check that first
    double *row_sums = row_sum(M);
    double magnum = row_sums[0];
    if (!is_uniform_vector(row_sums,M->rows,magnum)) {
        return 0;
    }
    free(row_sums); // tidy tidy
    
    // adding columns is slower, since it's more likely for the data to be
    // spatially distant
    double *col_sums = col_sum(M);
    if (!is_uniform_vector(col_sums,M->cols,magnum)) {
        return 0;
    }
    free(col_sums);
    
    double *diag_sums = diag_sum(M);
    return (diag_sums[0] == magnum) && (diag_sums[1] == magnum);
}
 int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {    
     int M = matrix.size();
     if (M == 0)
         return 0;
     int N = matrix[0].size();
     if (N == 0)
         return 0;
         
     bool flag = false;
     if (M > N) {
         swap(M, N);
         flag = true;
     }
         
     int res = INT_MIN;
     for (int up = 0; up < M; up++) { //O(M)
         vector<int> col_sum(N, 0);
         for (int down = up; down < M; down++) { //O(M)
             for (int j = 0; j < N; j++)
                 col_sum[j] += flag ? matrix[j][down] : matrix[down][j];
                 
             // Find the max subarray no more than K 
             set<int> squareSet;
             squareSet.insert(0);
             int curSum = 0, curMax = INT_MIN;
             for (int sum : col_sum) { //O(N)
                 curSum += sum;
                 auto it = squareSet.lower_bound(curSum - k);//O(logN)
                 if (it != squareSet.end())
                     curMax = max(curMax, curSum - *it);
             squareSet.insert(curSum);
         }
         res = max(res, curMax);
         }
     }
     return res;
 }