<commit_before>#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "pcapack.h"
#include "misc.h"
#include "lapack.h"


int pcapack_svd(bool inplace, const int nu, const int nv, int m, int n, double *x, double *s, double *u, double *vt)
{
  char jobz;
  int i;
  int info = 0;
  int lwork, *iwork;
  double tmp, *work, *x_cp;
  int minmn = m<n ? m : n;
  
  if (nu == 0 && nv == 0)
    jobz = 'n';
  else if ((nu == 0 && m >= n) || (nv == 0 && m < n))
    jobz = 'o';
  else if (nu <= minmn && nv <= minmn)
    jobz = 's';
  else
    jobz = 'a';
  
  
  if (likely(inplace))
    x_cp = x;
  else
  {
    x_cp = malloc(m*n * sizeof(*x_cp));
    memcpy(x_cp, x, m*n*sizeof(*x_cp));
  }
  
  iwork = malloc(8*minmn * sizeof(*iwork));
  
  lwork = -1;
  dgesdd_(&jobz, &m, &n, x_cp, &m, s, u, &m, vt, &minmn, &tmp, &lwork, iwork, &info);
  lwork = (int) tmp;
  work = malloc(lwork * sizeof(*work));
  dgesdd_(&jobz, &m, &n, x_cp, &m, s, u, &m, vt, &minmn, work, &lwork, iwork, &info);
  
  if (likely(!inplace)) free(x_cp);
  free(work);
  free(iwork);
  
  return info;
}



// m == n
int pcapack_eig(bool inplace, bool only_values, bool symmetric, int n, double *x, double *values, double *vectors)
{
  int info = 0;
  double *x_cp;
  // Passed to Fortran
  double worksize;
  int lwork, liwork;
  int *iwork;
  double *work;
  static int neg1 = -1;
  
  
  if (!inplace)
  {
    x_cp = malloc(n*n * sizeof(*x_cp));
    memcpy(x_cp, x, n*n*sizeof(double));
  }
  else
    x_cp = x;
  
  
  if (likely(symmetric))
  {
    char jobz;
    char uplo = 'U', trans = 'N';
    
    if (only_values) jobz = 'n';
    else             jobz = 'v';
    
    dsyevd_(&jobz, &uplo, &n, x_cp, &n, values, &worksize, &neg1, &liwork, &neg1, &info);
    
    lwork = (int) worksize;
    work = malloc(lwork * sizeof(*work));
    iwork = malloc(liwork * sizeof(*iwork));
    
    dsyevd_(&jobz, &uplo, &n, x_cp, &n, values, work, &lwork, iwork, &liwork, &info);
  }
  #if 0 // TODO
  else
  {
    char jobvl = 'n', jobvr;
    
    if (only_values)
      jobvr = 'v';
    else
      jobvr = 'n';
    
    
    dgeev_(jobvl, jobvr, &n, x, &m, wr, wi, vl, ldvl, vr, ldvr, work, lwork, info);
    
    
    dgeev_(jobvl, jobvr, n, a, lda, wr, wi, vl, ldvl, vr, ldvr, work, &neg1, info);
  }<commit_msg>consistency in fortran char case<commit_after>#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "pcapack.h"
#include "misc.h"
#include "lapack.h"


int pcapack_svd(bool inplace, const int nu, const int nv, int m, int n, double *x, double *s, double *u, double *vt)
{
  char jobz;
  int i;
  int info = 0;
  int lwork, *iwork;
  double tmp, *work, *x_cp;
  int minmn = m<n ? m : n;
  
  if (nu == 0 && nv == 0)
    jobz = 'n';
  else if ((nu == 0 && m >= n) || (nv == 0 && m < n))
    jobz = 'o';
  else if (nu <= minmn && nv <= minmn)
    jobz = 's';
  else
    jobz = 'a';
  
  
  if (likely(inplace))
    x_cp = x;
  else
  {
    x_cp = malloc(m*n * sizeof(*x_cp));
    memcpy(x_cp, x, m*n*sizeof(*x_cp));
  }
  
  iwork = malloc(8*minmn * sizeof(*iwork));
  
  lwork = -1;
  dgesdd_(&jobz, &m, &n, x_cp, &m, s, u, &m, vt, &minmn, &tmp, &lwork, iwork, &info);
  lwork = (int) tmp;
  work = malloc(lwork * sizeof(*work));
  dgesdd_(&jobz, &m, &n, x_cp, &m, s, u, &m, vt, &minmn, work, &lwork, iwork, &info);
  
  if (likely(!inplace)) free(x_cp);
  free(work);
  free(iwork);
  
  return info;
}



// m == n
int pcapack_eig(bool inplace, bool only_values, bool symmetric, int n, double *x, double *values, double *vectors)
{
  int info = 0;
  double *x_cp;
  // Passed to Fortran
  double worksize;
  int lwork, liwork;
  int *iwork;
  double *work;
  static int neg1 = -1;
  
  
  if (!inplace)
  {
    x_cp = malloc(n*n * sizeof(*x_cp));
    memcpy(x_cp, x, n*n*sizeof(double));
  }
  else
    x_cp = x;
  
  
  if (likely(symmetric))
  {
    char jobz;
    char uplo = 'u', trans = 'n';
    
    if (only_values) jobz = 'n';
    else             jobz = 'v';
    
    dsyevd_(&jobz, &uplo, &n, x_cp, &n, values, &worksize, &neg1, &liwork, &neg1, &info);
    
    lwork = (int) worksize;
    work = malloc(lwork * sizeof(*work));
    iwork = malloc(liwork * sizeof(*iwork));
    
    dsyevd_(&jobz, &uplo, &n, x_cp, &n, values, work, &lwork, iwork, &liwork, &info);
  }
  #if 0 // TODO
  else
  {
    char jobvl = 'n', jobvr;
    
    if (only_values)
      jobvr = 'v';
    else
      jobvr = 'n';
    
    
    dgeev_(jobvl, jobvr, &n, x, &m, wr, wi, vl, ldvl, vr, ldvr, work, lwork, info);
    
    
    dgeev_(jobvl, jobvr, n, a, lda, wr, wi, vl, ldvl, vr, ldvr, work, &neg1, info);
  }
{'fraction_non_alphanumeric': 0.18450184501845018, 'fraction_numerical': 0.006077707835901888, 'mean_word_length': 2.574864235841738, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1963705', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2062, 'n_words': 560}