<commit_before>/*
  Teem: Tools to process and visualize scientific data and images             .
  Copyright (C) 2011, 2010, 2009, 2008 <NAME>
  Copyright (C) 2010, 2009, 2008 <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public License
  (LGPL) as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  The terms of redistributing and/or modifying this software also
  include exceptions to the LGPL that facilitate static linking.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this library; if not, write to Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "tijk.h"
#include "privateTijk.h"

#include "convertQuietPush.h"

/* Functions for symmetric tensor approximation */

/* a coarse sampling of the unit semicircle */
const unsigned int _tijk_max_candidates_2d=8;

#define _CANDIDATES_2D(TYPE, SUF)                 \
  static TYPE _candidates_2d_##SUF[16] = {        \
    1.0, 0.0,                                     \
    0.92387953251128674, 0.38268343236508978,     \
    0.70710678118654757, 0.70710678118654746,     \
    0.38268343236508984, 0.92387953251128674,     \
    0.0, 1.0,                                     \
    -0.38268343236508973, 0.92387953251128674,    \
    -0.70710678118654746, 0.70710678118654757,    \
    -0.92387953251128674, 0.38268343236508989     \
  };
_CANDIDATES_2D(double, d)
_CANDIDATES_2D(float, f)

/* a coarse sampling of the unit sphere */
const unsigned int _tijk_max_candidates_3d=30;

#define _CANDIDATES_3D(TYPE, SUF)             \
  static TYPE _candidates_3d_##SUF[90] = {    \
    -0.546405, 0.619202, 0.563943,            \
    -0.398931,-0.600006, 0.693432,            \
    0.587973, 0.521686, 0.618168,             \
    0.055894,-0.991971,-0.113444,             \
    -0.666933,-0.677984, 0.309094,            \
    0.163684, 0.533013, 0.830123,             \
    0.542826, 0.133898, 0.829102,             \
    -0.074751,-0.350412, 0.933608,            \
    0.845751, -0.478624,-0.235847,            \
    0.767148,-0.610673, 0.196372,             \
    -0.283810, 0.381633, 0.879663,            \
    0.537228,-0.616249, 0.575868,             \
    -0.711387, 0.197778, 0.674398,            \
    0.886511, 0.219025, 0.407586,             \
    0.296061, 0.842985, 0.449136,             \
    -0.937540,-0.340990, 0.068877,            \
    0.398833, 0.917023, 0.000835,             \
    0.097278,-0.711949, 0.695460,             \
    -0.311534, 0.908623,-0.278121,            \
    -0.432043,-0.089758, 0.897375,            \
    -0.949980, 0.030810, 0.310788,            \
    0.146722,-0.811981,-0.564942,             \
    -0.172201,-0.908573, 0.380580,            \
    0.507209,-0.848578,-0.150513,             \
    -0.730808,-0.654136,-0.194999,            \
    0.077744, 0.094961, 0.992441,             \
    0.383976,-0.293959, 0.875300,             \
    0.788208,-0.213656, 0.577130,             \
    -0.752333,-0.301447, 0.585769,            \
    -0.975732, 0.165497,-0.143382             \
  };

_CANDIDATES_3D(double, d)
_CANDIDATES_3D(float, f)

/* Produces a rough estimate of the position and value of the ABSOLUTE
 * maximum of the homogeneous form.
 * s and v will be set to the corresponding value and (unit-length) direction.
 * ten is the input tensor, type is its type.
 * returns 0 upon success, 1 upon erroneous parameters. */
#define _TIJK_INIT_RANK1(TYPE, SUF, DIM)                            \
  int                                                               \
  tijk_init_rank1_##DIM##d_##SUF(TYPE *s, TYPE *v, const TYPE *ten, \
                                 const tijk_type *type) {           \
    TYPE absmax=-1;                                                 \
    unsigned int i;                                                 \
    TYPE *candidate=_candidates_##DIM##d_##SUF;                     \
    if (type->dim!=DIM || type->sym==NULL)                          \
      return 1;                                                     \
    for (i=0; i<_tijk_max_candidates_##DIM##d; i++) {               \
      TYPE val=(*type->sym->s_form_##SUF)(ten, candidate);          \
      TYPE absval=fabs(val);                                        \
      if (absval>absmax) {                                          \
        absmax=absval;                                              \
        *s=val;                                                     \
        ELL_##DIM##V_COPY(v, candidate);                            \
      }                                                             \
      candidate+=DIM;                                               \
    }                                                               \
    return 0;                                                       \
  }

_TIJK_INIT_RANK1(double, d, 2)
_TIJK_INIT_RANK1(float, f, 2)
_TIJK_INIT_RANK1(double, d, 3)
_TIJK_INIT_RANK1(float, f, 3)

/* Produces a rough estimate of the position and value of the SIGNED
 * maximum of the homogeneous form.
 * s and v will be set to the corresponding value and (unit-length) direction.
 * ten is the input tensor, type is its type.
 * returns 0 upon success, 1 upon erroneous parameters.
 */
#define _TIJK_INIT_MAX(TYPE, SUF, DIM)                            \
  int                                                             \
  tijk_init_max_##DIM##d_##SUF(TYPE *s, TYPE *v, const TYPE *ten, \
                               const tijk_type *type) {           \
    TYPE max=0;                                                   \
    unsigned int i;                                               \
    TYPE *candidate=_candidates_##DIM##d_##SUF;                   \
    if (type->dim!=DIM || type->sym==NULL)                        \
      return 1;                                                   \
    *s=max=(*type->sym->s_form_##SUF)(ten, candidate);            \
    ELL_##DIM##V_COPY(v, candidate);                              \
    for (i=1; i<_tijk_max_candidates_##DIM##d; i++) {             \
      TYPE val;                                                   \
      candidate+=DIM;                                             \
      val=(*type->sym->s_form_##SUF)(ten, candidate);             \
      if (val>max) {                                              \
        max=val;                                                  \
        *s=val;                                                   \
        ELL_##DIM##V_COPY(v, candidate);                          \
      }                                                           \
    }                                                             \
    return 0;                                                     \
  }

_TIJK_INIT_MAX(double, d, 2)
_TIJK_INIT_MAX(float, f, 2)
_TIJK_INIT_MAX(double, d, 3)
_TIJK_INIT_MAX(float, f, 3)

static const tijk_refine_rank1_parm refine_rank1_parm_default = {
  1e-10, 1e-4, 0.3, 0.9, 0.5, 50};

tijk_refine_rank1_parm *tijk_refine_rank1_parm_new() {
  tijk_refine_rank1_parm *parm;
  parm = (tijk_refine_rank1_parm *)malloc(sizeof(tijk_refine_rank1_parm));
  if (parm) {
    memcpy(parm,&refine_rank1_parm_default,sizeof(tijk_refine_rank1_parm));
  }
  return parm;<commit_msg>tijk: Decided that I want slightly higher default accuracy<commit_after>/*
  Teem: Tools to process and visualize scientific data and images             .
  Copyright (C) 2011, 2010, 2009, 2008 <NAME>
  Copyright (C) 2010, 2009, 2008 <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public License
  (LGPL) as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  The terms of redistributing and/or modifying this software also
  include exceptions to the LGPL that facilitate static linking.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this library; if not, write to Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "tijk.h"
#include "privateTijk.h"

#include "convertQuietPush.h"

/* Functions for symmetric tensor approximation */

/* a coarse sampling of the unit semicircle */
const unsigned int _tijk_max_candidates_2d=8;

#define _CANDIDATES_2D(TYPE, SUF)                 \
  static TYPE _candidates_2d_##SUF[16] = {        \
    1.0, 0.0,                                     \
    0.92387953251128674, 0.38268343236508978,     \
    0.70710678118654757, 0.70710678118654746,     \
    0.38268343236508984, 0.92387953251128674,     \
    0.0, 1.0,                                     \
    -0.38268343236508973, 0.92387953251128674,    \
    -0.70710678118654746, 0.70710678118654757,    \
    -0.92387953251128674, 0.38268343236508989     \
  };
_CANDIDATES_2D(double, d)
_CANDIDATES_2D(float, f)

/* a coarse sampling of the unit sphere */
const unsigned int _tijk_max_candidates_3d=30;

#define _CANDIDATES_3D(TYPE, SUF)             \
  static TYPE _candidates_3d_##SUF[90] = {    \
    -0.546405, 0.619202, 0.563943,            \
    -0.398931,-0.600006, 0.693432,            \
    0.587973, 0.521686, 0.618168,             \
    0.055894,-0.991971,-0.113444,             \
    -0.666933,-0.677984, 0.309094,            \
    0.163684, 0.533013, 0.830123,             \
    0.542826, 0.133898, 0.829102,             \
    -0.074751,-0.350412, 0.933608,            \
    0.845751, -0.478624,-0.235847,            \
    0.767148,-0.610673, 0.196372,             \
    -0.283810, 0.381633, 0.879663,            \
    0.537228,-0.616249, 0.575868,             \
    -0.711387, 0.197778, 0.674398,            \
    0.886511, 0.219025, 0.407586,             \
    0.296061, 0.842985, 0.449136,             \
    -0.937540,-0.340990, 0.068877,            \
    0.398833, 0.917023, 0.000835,             \
    0.097278,-0.711949, 0.695460,             \
    -0.311534, 0.908623,-0.278121,            \
    -0.432043,-0.089758, 0.897375,            \
    -0.949980, 0.030810, 0.310788,            \
    0.146722,-0.811981,-0.564942,             \
    -0.172201,-0.908573, 0.380580,            \
    0.507209,-0.848578,-0.150513,             \
    -0.730808,-0.654136,-0.194999,            \
    0.077744, 0.094961, 0.992441,             \
    0.383976,-0.293959, 0.875300,             \
    0.788208,-0.213656, 0.577130,             \
    -0.752333,-0.301447, 0.585769,            \
    -0.975732, 0.165497,-0.143382             \
  };

_CANDIDATES_3D(double, d)
_CANDIDATES_3D(float, f)

/* Produces a rough estimate of the position and value of the ABSOLUTE
 * maximum of the homogeneous form.
 * s and v will be set to the corresponding value and (unit-length) direction.
 * ten is the input tensor, type is its type.
 * returns 0 upon success, 1 upon erroneous parameters. */
#define _TIJK_INIT_RANK1(TYPE, SUF, DIM)                            \
  int                                                               \
  tijk_init_rank1_##DIM##d_##SUF(TYPE *s, TYPE *v, const TYPE *ten, \
                                 const tijk_type *type) {           \
    TYPE absmax=-1;                                                 \
    unsigned int i;                                                 \
    TYPE *candidate=_candidates_##DIM##d_##SUF;                     \
    if (type->dim!=DIM || type->sym==NULL)                          \
      return 1;                                                     \
    for (i=0; i<_tijk_max_candidates_##DIM##d; i++) {               \
      TYPE val=(*type->sym->s_form_##SUF)(ten, candidate);          \
      TYPE absval=fabs(val);                                        \
      if (absval>absmax) {                                          \
        absmax=absval;                                              \
        *s=val;                                                     \
        ELL_##DIM##V_COPY(v, candidate);                            \
      }                                                             \
      candidate+=DIM;                                               \
    }                                                               \
    return 0;                                                       \
  }

_TIJK_INIT_RANK1(double, d, 2)
_TIJK_INIT_RANK1(float, f, 2)
_TIJK_INIT_RANK1(double, d, 3)
_TIJK_INIT_RANK1(float, f, 3)

/* Produces a rough estimate of the position and value of the SIGNED
 * maximum of the homogeneous form.
 * s and v will be set to the corresponding value and (unit-length) direction.
 * ten is the input tensor, type is its type.
 * returns 0 upon success, 1 upon erroneous parameters.
 */
#define _TIJK_INIT_MAX(TYPE, SUF, DIM)                            \
  int                                                             \
  tijk_init_max_##DIM##d_##SUF(TYPE *s, TYPE *v, const TYPE *ten, \
                               const tijk_type *type) {           \
    TYPE max=0;                                                   \
    unsigned int i;                                               \
    TYPE *candidate=_candidates_##DIM##d_##SUF;                   \
    if (type->dim!=DIM || type->sym==NULL)                        \
      return 1;                                                   \
    *s=max=(*type->sym->s_form_##SUF)(ten, candidate);            \
    ELL_##DIM##V_COPY(v, candidate);                              \
    for (i=1; i<_tijk_max_candidates_##DIM##d; i++) {             \
      TYPE val;                                                   \
      candidate+=DIM;                                             \
      val=(*type->sym->s_form_##SUF)(ten, candidate);             \
      if (val>max) {                                              \
        max=val;                                                  \
        *s=val;                                                   \
        ELL_##DIM##V_COPY(v, candidate);                          \
      }                                                           \
    }                                                             \
    return 0;                                                     \
  }

_TIJK_INIT_MAX(double, d, 2)
_TIJK_INIT_MAX(float, f, 2)
_TIJK_INIT_MAX(double, d, 3)
_TIJK_INIT_MAX(float, f, 3)

static const tijk_refine_rank1_parm refine_rank1_parm_default = {
  1e-10, 1e-6, 0.3, 0.9, 0.5, 50};

tijk_refine_rank1_parm *tijk_refine_rank1_parm_new() {
  tijk_refine_rank1_parm *parm;
  parm = (tijk_refine_rank1_parm *)malloc(sizeof(tijk_refine_rank1_parm));
  if (parm) {
    memcpy(parm,&refine_rank1_parm_default,sizeof(tijk_refine_rank1_parm));
  }
  return parm;
{'fraction_non_alphanumeric': 0.10157218344314697, 'fraction_numerical': 0.12525278883162633, 'mean_word_length': 1.2201303403330919, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 104, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1963768', 'n_tokens_mistral': 5888, 'n_tokens_neox': 4728, 'n_words': 1061}