<commit_before>// -*- c++ -*-
// UjoImro, 2013
// Experimental code for the CARP Project
// Copyright (c) RealEyes, 2013
// This is a c-implementation of the PCA->MLP response map calculation

#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>

#include "mlp_impl.h"

float GetValueFloat(MatFloat self, int row, int col);

void printMatFloat(MatFloat mat, char *name) {
  printf("%s = [\n", name);

  int q, w;

  for (q = 0; q < mat.rows; q++) {
    printf("[ ");
    for (w = 0; w < mat.cols; w++) {
      printf("%f, ", GetValueFloat(mat, q, w));
    }
    printf(" ]\n");
  }

  printf("]\n");

  return;
}

void freeMLP(mlp *classifier) {
  freeMatFloat(&classifier->m_wIn);
  freeMatFloat(&classifier->m_wOut);
  freeMatFloat(&classifier->m_U);
}

MatFloat CreateMatFloat(int rows, int cols) {
  MatFloat result;
  assert(rows > 0);
  assert(cols > 0);

  result.data = NULL;
  result.data = (float *)malloc(sizeof(float) * rows * cols);
  assert(result.data);
  result.rows = rows;
  result.cols = cols;
  result.step = cols;
  result.start = 0;

  return result;
}

MatChar CreateMatChar(int rows, int cols) {
  MatChar result;
  assert(rows > 0);
  assert(cols > 0);

  result.data = NULL;
  result.data = (uint8_t *)malloc(sizeof(uint8_t) * rows * cols);
  assert(result.data);
  result.rows = rows;
  result.cols = cols;
  result.step = cols;
  result.start = 0;

  return result;
}

static void transposeFloat(MatFloat input, MatFloat *output) {
  int q, w;
  assert(output->rows == input.cols);
  assert(output->cols == input.rows);
  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++)
      output->data[w * output->step + q + output->start] =
          input.data[q * input.step + w + input.start];

  return;
}

static float meanChar(MatChar input) {
  assert(input.data);
  int q, w;
  float sum = 0;
  float c = 0; // kahan summation

  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++) {
      float y = input.data[q * input.step + w + input.start] - c;
      float t = sum + y;
      c = (t - sum) - y;
      sum = t;
    }

  return sum / (input.rows * input.cols);
}

static uint8_t min(uint8_t a, uint8_t b) { return a < b ? a : b; }

static uint8_t minChar(MatChar input) {
  assert(input.data);
  int q, w;
  uint8_t minvalue = 255;

  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++)
      minvalue = min(minvalue, input.data[q * input.step + w + input.start]);

  return minvalue;
}

static uint8_t max(uint8_t a, uint8_t b) { return a > b ? a : b; }

static uint8_t maxChar(MatChar input) {
  assert(input.data);
  int q, w;
  uint8_t maxvalue = 0;

  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++)
      maxvalue = max(maxvalue, input.data[q * input.step + w + input.start]);

  return maxvalue;
}

static MatChar GetBlockChar(MatChar self, int row_from, int row_to,
                            int col_from, int col_to) {
  assert(row_from >= 0);
  assert(col_from >= 0);
  assert(row_from < row_to);<commit_msg>Marked input matrices for maxChar(), minChar() and meanChar() as const matrices (read only)<commit_after>// -*- c++ -*-
// UjoImro, 2013
// Experimental code for the CARP Project
// Copyright (c) RealEyes, 2013
// This is a c-implementation of the PCA->MLP response map calculation

#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>

#include "mlp_impl.h"

float GetValueFloat(MatFloat self, int row, int col);

void printMatFloat(MatFloat mat, char *name) {
  printf("%s = [\n", name);

  int q, w;

  for (q = 0; q < mat.rows; q++) {
    printf("[ ");
    for (w = 0; w < mat.cols; w++) {
      printf("%f, ", GetValueFloat(mat, q, w));
    }
    printf(" ]\n");
  }

  printf("]\n");

  return;
}

void freeMLP(mlp *classifier) {
  freeMatFloat(&classifier->m_wIn);
  freeMatFloat(&classifier->m_wOut);
  freeMatFloat(&classifier->m_U);
}

MatFloat CreateMatFloat(int rows, int cols) {
  MatFloat result;
  assert(rows > 0);
  assert(cols > 0);

  result.data = NULL;
  result.data = (float *)malloc(sizeof(float) * rows * cols);
  assert(result.data);
  result.rows = rows;
  result.cols = cols;
  result.step = cols;
  result.start = 0;

  return result;
}

MatChar CreateMatChar(int rows, int cols) {
  MatChar result;
  assert(rows > 0);
  assert(cols > 0);

  result.data = NULL;
  result.data = (uint8_t *)malloc(sizeof(uint8_t) * rows * cols);
  assert(result.data);
  result.rows = rows;
  result.cols = cols;
  result.step = cols;
  result.start = 0;

  return result;
}

static void transposeFloat(MatFloat input, MatFloat *output) {
  int q, w;
  assert(output->rows == input.cols);
  assert(output->cols == input.rows);
  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++)
      output->data[w * output->step + q + output->start] =
          input.data[q * input.step + w + input.start];

  return;
}

static float meanChar(const MatChar input) {
  assert(input.data);
  int q, w;
  float sum = 0;
  float c = 0; // kahan summation

  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++) {
      float y = input.data[q * input.step + w + input.start] - c;
      float t = sum + y;
      c = (t - sum) - y;
      sum = t;
    }

  return sum / (input.rows * input.cols);
}

static uint8_t min(uint8_t a, uint8_t b) { return a < b ? a : b; }

static uint8_t minChar(const MatChar input) {
  assert(input.data);
  int q, w;
  uint8_t minvalue = 255;

  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++)
      minvalue = min(minvalue, input.data[q * input.step + w + input.start]);

  return minvalue;
}

static uint8_t max(uint8_t a, uint8_t b) { return a > b ? a : b; }

static uint8_t maxChar(const MatChar input) {
  assert(input.data);
  int q, w;
  uint8_t maxvalue = 0;

  for (q = 0; q < input.rows; q++)
    for (w = 0; w < input.cols; w++)
      maxvalue = max(maxvalue, input.data[q * input.step + w + input.start]);

  return maxvalue;
}

static MatChar GetBlockChar(MatChar self, int row_from, int row_to,
                            int col_from, int col_to) {
  assert(row_from >= 0);
  assert(col_from >= 0);
  assert(row_from < row_to);
{'fraction_non_alphanumeric': 0.16207117665918563, 'fraction_numerical': 0.014107085604360373, 'mean_word_length': 2.9288413098236776, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6573589', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2345, 'n_words': 662}