Exemplo n.º 1
0
LVAL xsaxpy(V)
{
  LVAL result, next, tx, a, x, y;
  int i, j, m, n, start, end, lower;
  double val;
  
  a = getdarraydata(xlgamatrix());
  x = xlgaseq();
  y = xlgaseq();
  lower = (moreargs() && xlgetarg() != NIL) ? TRUE : FALSE;
  
  n = seqlen(x);
  m = seqlen(y);
  if (lower && m != n)
    xlfail("dimensions do not match");
  
  xlsave1(result);
  result = mklist(m, NIL);
  for (i = 0, start = 0, next = result;
       i < m;
       i++, start += n, next = cdr(next)) {
    val = makefloat(getnextelement(&y, i));
    end = (lower) ? i +1 : n;
    for (j = 0, tx = x; j < end; j++) {
      val += makefloat(getnextelement(&tx, j)) 
	* makefloat(gettvecelement(a, start + j));
    }
    rplaca(next, cvflonum((FLOTYPE) val));
  }
  xlpop();
  return(result);
}
Exemplo n.º 2
0
int seqlen(long long n)
{
    int ans;

    if ( n < MAX )
    {
        ans = dp[n];
        if ( ans == -1 )
        {
            if ( n&1 )  ans = 1 + seqlen(3*n+1);
            else        ans = 1 + seqlen(n/2);
            dp[n] = ans;
        }
    }
    else
    {
        ans = 0;
        while ( n >= MAX )
        {
            if ( n&1 )  n = 3*n+1;
            else        n /= 2;
            ++ans;
        }
        ans += seqlen(n);
    }

    return ans;
}
Exemplo n.º 3
0
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif

    int l, h, best, cand, ans;

	memset(dp, -1, sizeof dp);
	dp[0] = dp[1] = 0;

	while ( scanf("%d %d", &l, &h), l || h )
	{
	    if ( l > h ) l ^= h, h ^= l, l ^= h; // swap
	    ans = l, best = ( l == 1 ? 3 : seqlen(l) );
	    for(int i = l+1; i <= h; ++i)
	    {
	        cand = seqlen(i);
	        if ( cand > best ) ans = i, best = cand;
	    }
	    printf("Between %d and %d, %d generates the longest sequence of %d values.\n", l, h, ans, best);
	}

    return 0;
}
Exemplo n.º 4
0
PartialDP* createPartialDP(unsigned char* attribute, int costIndel, int x_value){

    PartialDP* partial_dp = malloc(sizeof(PartialDP));

    if (partial_dp != NULL){
        partial_dp->query = attribute;
        partial_dp->nbCol = seqlen(attribute) + 1;
        partial_dp->current_line = 1;
        partial_dp->A_etoile = 0;
        partial_dp->colg = 1;
        partial_dp->cold = partial_dp->nbCol-1;
        partial_dp->treshold = x_value;

        partial_dp->data = malloc(partial_dp->nbCol * sizeof(int));
        if (partial_dp->data != NULL){
            int i;
            partial_dp->data[0] = 0;

            for (i=1;i<partial_dp->nbCol;i++){
                if (getNucl(partial_dp->query, i-1) == INT_MAX) partial_dp->data[i] = partial_dp->data[i-1];
                else partial_dp->data[i] = partial_dp->data[i-1] + costIndel;
            }
        }
        else {
            fprintf (stderr, "insufficient memory to create a partial_dp->data of size %d\n", partial_dp->nbCol+1);
            exit (EXIT_FAILURE);
        }

        return partial_dp;
    }
    else {
        fprintf (stderr, "insufficient memory to create a partial_dp \n");
        exit (EXIT_FAILURE);
    }
}
Exemplo n.º 5
0
LVAL xsfft(V)
{
  LVAL data, result, x, work;
  int n, isign;
  
  data = xlgaseq();
  isign = (moreargs() && xlgetarg() != NIL) ? -1.0 : 1.0; 
  xllastarg();
  
  /* check and convert the data */
  n = seqlen(data);
  if (n <= 0)
    xlfail("not enough data");

  xlstkcheck(2);
  xlsave(x);
  xlsave(work);
  x = gen2linalg(data, n, 1, s_c_dcomplex, FALSE);
  work = mktvec(4 * n + 15, s_c_double);

  cfft(n, REDAT(x), REDAT(work), isign);

  result = listp(x) ? coerce_to_list(x) : coerce_to_tvec(x, s_true);
  xlpopn(2);

  return result;
}
Exemplo n.º 6
0
LVAL xssample(V)
{
  LVAL x, result, temp, elem;
  int n, N, replace, i, j;
  
  x = xlgaseq();
  n = getfixnum(xlgafixnum());
  N = seqlen(x);
  replace = (moreargs()) ? (xlgetarg() != NIL) : FALSE;
  xllastarg();

  if (! replace && n > N) n = N;

  xlstkcheck(4);
  xlprotect(x);
  xlsave(result);
  xlsave(elem);
  xlsave(temp);
  x = (listp(x)) ? coerce_to_tvec(x, s_true) : copyvector(x);
  result = NIL;
  if (N > 0 && n > 0) {
    for (i = 0; i < n; i++) {
      j = (replace) ? osrand(N) : i + osrand(N - i);
      elem = gettvecelement(x, j);
      result = cons(elem, result);
      if (! replace) {           /* swap elements i and j */
        temp = gettvecelement(x, i);
        settvecelement(x, i, elem);
        settvecelement(x, j, temp);
      }
    }
  }
  xlpopn(4);
  return(result);
}
Exemplo n.º 7
0
static void dofile(FILE * inf, char name[])
{
    unsigned char seq[7];
    uint32_t cdpt;
    size_t n = 0;
    size_t l = 0;
    int len;

    memset(seq, '\0', sizeof(seq) * sizeof(seq[0]));

    while (next_sequence(inf, seq) != -1) {
        n += seqlen(seq[0]);
        sequence_to_ucs4(seq, &cdpt);
        if ('\n' == cdpt)
            l++;
        cdpt = rot(cdpt);
        len = utf8encode(cdpt, (uint8_t *) seq);
        fwrite(seq, 1, len, stdout);
    }

    if (!valid_sequence(seq) && !feof(inf))
        fprintf(stderr,
                "rot32768: invalid UTF-8 sequence at line %llu (byte %llu) in %s\n",
                (unsigned long long int) l, (unsigned long long int) n,
                name);
}
Exemplo n.º 8
0
void LongSeq::findLongSeq()
{
    std::vector<int> seqlen(nelements, 0);
    int i = 0;
    int max;
    int index;
    seqlen[0] = 0;
    max = seqlen[0];
    index = 0;
    for (i = 1; i < nelements; i++)
    {
        if (elements[i-1] < elements[i])
        {
            seqlen[i] = seqlen[i-1] + 1;
            if (seqlen[i] > max)
            {
                max = seqlen[i];
                index = i - max;
            }
        }
        else
            seqlen[i] = 0;
    }
    printf ("length of longest sequence:%d index: %d\n", max, index);
}
Exemplo n.º 9
0
LVAL xssurface_contour(V)
{
  LVAL s1, s2, mat, result;
  LVAL x, y, z;
  double *dx, *dy, *dz;
  double v;
  int i, j, n, m;
  
  s1 = xlgaseq();
  s2 = xlgaseq();
  mat = xlgamatrix();
  v = makefloat(xlgetarg());
  xllastarg();
    
  n = seqlen(s1);
  m = seqlen(s2);
  if (n != numrows(mat) || m != numcols(mat))
    xlfail("dimensions do not match");

  xlstkcheck(4);
  xlsave(x);
  xlsave(y);
  xlsave(z);
  xlsave(result);

  x = gen2linalg(s1,  n, 1, s_c_double, FALSE); dx = REDAT(x);
  y = gen2linalg(s2,  m, 1, s_c_double, FALSE); dy = REDAT(y);
  z = gen2linalg(mat, n, m, s_c_double, FALSE); dz = REDAT(z);
  result = NIL;

  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < m - 1; j++) {
      result = add_contour_point(m, i,   j,   i,   j+1, dx, dy, dz, v, result);
      result = add_contour_point(m, i,   j+1, i+1, j+1, dx, dy, dz, v, result);
      result = add_contour_point(m, i+1, j+1, i+1, j,   dx, dy, dz, v, result);
      result = add_contour_point(m, i+1, j,   i,   j,   dx, dy, dz, v, result);
    }
  }
  xlpopn(4);
  
  return(result);
}
Exemplo n.º 10
0
int computePartialDP(PartialDP* partial_dp, unsigned char* graphString, int costIndel, int costMismatch, int costMatch){

    int j;
    int graphString_lg = seqlen(graphString);

    while (partial_dp->current_line<partial_dp->nbCol && partial_dp->current_line<=graphString_lg && partial_dp->colg <= partial_dp->cold){
        j=partial_dp->colg;
        int tmp = partial_dp->data[j-1];

        if (getNucl(graphString, j-1) != INT_MAX) partial_dp->data[0] += costIndel;

        while (j<=min(partial_dp->cold+1, partial_dp->nbCol-1)){
            int cmp = seqcmp(graphString, partial_dp->current_line-1, partial_dp->query, j-1, costMatch, costMismatch);

            int tmp2 = tmp;
            tmp = partial_dp->data[j];

            if (cmp == INT_MAX) partial_dp->data[j] = partial_dp->data[j-1];
            else if (cmp != INT_MIN) partial_dp->data[j]=max3(tmp2+cmp, partial_dp->data[j]+costIndel, partial_dp->data[j-1]+costIndel);

            if (partial_dp->data[j] > partial_dp->A_etoile) partial_dp->A_etoile = partial_dp->data[j];
            else if (partial_dp->data[j] < partial_dp->A_etoile - partial_dp->treshold) partial_dp->data[j] = INT_MIN+2;

            //printf("colg = %d \t j = %d \t Cmp: %d \t Alt1: %d \t Alt2: %d \t Alt3: %d \t Res: %d\n", partial_dp->colg, j, cmp, tmp+cmp, partial_dp->data[j]+costIndel, partial_dp->data[j-1]+costIndel, partial_dp->data[j]);

            j++;

        }

        while (partial_dp->colg <= partial_dp->cold && partial_dp->data[partial_dp->colg] == INT_MIN+2){
            partial_dp->colg++;
            if (partial_dp->cold < partial_dp->nbCol -1) partial_dp->cold++;
        }

       while (partial_dp->colg <= partial_dp->cold && partial_dp->data[partial_dp->cold] == INT_MIN+2){
            partial_dp->cold--;
        }

        //printPartialDP(partial_dp);

        partial_dp->current_line++;
    }

    int res =INT_MIN+2;
    for(j=1;j<partial_dp->nbCol;j++){
        if(res<partial_dp->data[j]) res = partial_dp->data[j];
    }

    return res;
}
Exemplo n.º 11
0
static VOID set_internal_transformation P3C(int, vars, LVAL, m, LVAL, b)
{
  int i, j, k, rows, cols;
  LVAL data;
  
  if (vars <= 0) return;
  if (vars > maxvars) {
    maxvars = 0;
    StFree(transformdata);
    StFree(transform);
    StFree(inbasis);
    transformdata = (double *) StCalloc(vars * vars, sizeof(double));
    transform = (double **) StCalloc(vars, sizeof(double *));
    for (i = 0; i < vars; i++) transform[i] = transformdata + vars * i;
    inbasis = (int *) StCalloc(vars, sizeof(double));
    maxvars = vars;
  }
  
  if (! matrixp(m)) xlerror("not a matrix", m);
  rows = numrows(m);
  cols = numcols(m);
  if (rows > vars) rows = vars;
  if (cols > vars) cols = vars;
  if (rows != cols) xlerror("bad transformation matrix", m);

  /* fill in upper left corner of transform from m; rest is identity */
  data = getdarraydata(m);
  for (i = 0, k = 0; i < rows; i++) {
    for (j = 0; j < cols; j++, k++)
      transform[i][j] = makefloat(gettvecelement(data, k));
    for (j = cols; j < vars; j++)
      transform[i][j] = (i == j) ? 1.0 : 0.0;
  }
  for (i = rows; i < vars; i++)
    for (j = 0; j < vars; j++)
      transform[i][j] = (i == j) ? 1.0 : 0.0;
    
  /* figure out basis elements using b and size of m */
  if (b != NIL) {
    if (! seqp(b)) xlerror("not a sequence", b);
    if (seqlen(b) != rows) xlerror("wrong length for basis", b);
    for (i = 0; i < rows; i++)
      inbasis[i] = (getnextelement(&b, i) != NIL) ? TRUE : FALSE;
  }
  else for (i = 0; i < rows; i++) inbasis[i] = TRUE;
  for (i = rows; i < vars; i++) inbasis[i] = FALSE;
}
Exemplo n.º 12
0
/* compute the length of the result sequence */
LOCAL int findmaprlen P1C(LVAL, args)
{
  LVAL next, e;
  int len, rlen;

  for (rlen = -1, next = args; consp(next); next = cdr(next)) {
    e = car(next);
    if (! listp(e) && ! vectorp(e) && ! tvecp(e))
      xlbadtype(car(next));
    len = seqlen(e);
    if (rlen == -1)
      rlen = len;
    else
      rlen = (len < rlen) ? len : rlen;
  }
  return(rlen);
}
Exemplo n.º 13
0
unsigned char* reverseSequence(unsigned char* seq, int complement){
    int i=0, lg, seq_lg = seqlen(seq);

    //Calcul de la longueur du tableau de caractère
    lg = strlen(seq);
    char *newSeq = malloc((lg+1)* sizeof(unsigned char));

    if (newSeq != NULL){
        if (complement==0){
            for (i=0; i<lg; i++){
                newSeq[i] = reverseCompChar(seq[i]);
            }
            if (seq_lg%2==1) newSeq[i-1] &= MASK_1_0;
            newSeq[lg] = '\0';
            return newSeq;
        }

        if (lg > 1){
            for (i=0; i<lg/2; i++){
                newSeq[i] = reverseCompChar(seq[lg-i-1]);
                newSeq[lg-i-1] = reverseCompChar(seq[i]);
            }

            if (lg%2 == 1) newSeq[i] = reverseCompChar(seq[i]);
            newSeq[lg] = '\0';

            if (seq_lg%2 == 1){
                char* res = subSequence(newSeq, 1, (lg*2)-1);
                free(newSeq);
                return res;
            }
        }
        else{
            newSeq[i] = reverseCompChar(seq[i]);
            if (seq_lg%2==1) newSeq[i] <<= 4;
            newSeq[lg] = '\0';
        }

        return newSeq;
    }
    else {
        fprintf (stderr, "Insufficient memory\n");
        exit (EXIT_FAILURE);
    }
}
Exemplo n.º 14
0
/* pat; seq; 
 *      body;;
 *
 * =>
 *      .pseudo = seqinit
 *      jmp :cond
 *      :body
 *           ...body...
 *      :step
 *           ...step...
 *      :cond
 *           ...cond...
 *           cjmp (cond) :match :end
 *      :match
 *           ...match...
 *           cjmp (match) :body :step
 *      :end
 */
static void simpiter(Simp *s, Node *n)
{
    Node *lbody, *lstep, *lcond, *lmatch, *lend;
    Node *idx, *len, *dcl, *seq, *val, *done;
    Node *zero;

    lbody = genlbl();
    lstep = genlbl();
    lcond = genlbl();
    lmatch = genlbl();
    lend = genlbl();

    lappend(&s->loopstep, &s->nloopstep, lstep);
    lappend(&s->loopexit, &s->nloopexit, lend);

    zero = mkintlit(n->line, 0);
    zero->expr.type = tyintptr;

    seq = rval(s, n->iterstmt.seq, NULL);
    idx = gentemp(s, n, tyintptr, &dcl);
    declarelocal(s, dcl);

    /* setup */
    append(s, assign(s, idx, zero));
    jmp(s, lcond);
    simp(s, lbody);
    /* body */
    simp(s, n->iterstmt.body);
    /* step */
    simp(s, lstep);
    simp(s, assign(s, idx, addk(idx, 1)));
    /* condition */
    simp(s, lcond);
    len = seqlen(s, seq, tyintptr);
    done = mkexpr(n->line, Olt, idx, len, NULL);
    cjmp(s, done, lmatch, lend);
    simp(s, lmatch);
    val = load(idxaddr(s, seq, idx));
    umatch(s, n->iterstmt.elt, val, val->expr.type, lbody, lstep);
    simp(s, lend);

    s->nloopstep--;
    s->nloopexit--;
}
Exemplo n.º 15
0
unsigned char* subSequence(unsigned char* seq, int posDeb, int posFin){

    int i, k=0, lg, newSeq_lg, seq_lg, dec;
    unsigned char tmp1;
    unsigned char* newSeq;

    //Calcul de la longueur du tableau de caractère
    newSeq_lg = posFin-posDeb+1;
    seq_lg = seqlen(seq);
    dec = newSeq_lg%2;
    lg = newSeq_lg/2 + (dec != 0);

    newSeq = malloc((lg+1) * sizeof(unsigned char));

    if (newSeq != NULL){
        if (posDeb%2 == 0){
            for (i = posDeb/2; i<posFin/2; i++){
                newSeq[k] = seq[i];
                k++;
            }
            if (dec == 1) newSeq[k] = seq[i] & MASK_1_0;
            else newSeq[k] = seq[i];
        }
        else {
            for (i = posDeb/2; i<posFin/2; i++){
                tmp1 = seq[i] << 4;
                if (seq_lg > 2) tmp1 |= (seq[i+1] >> 4);
                newSeq[k] = tmp1;
                k++;
            }
            if (dec == 1) newSeq[k] = seq[i] << 4;
        }

        newSeq[lg] = '\0';

        return newSeq;
    }
    else {
Exemplo n.º 16
0
static VOID base_mean P3C(int *, count, Number *, mean, LVAL, x)
{
  LVAL y;
  Number num;
  double c, p, q;
  int i, n;

  if (! compoundp(x)) {
    c = *count; p = c / (c + 1.0); q = 1.0 - p;
    make_number(&num, x);
    mean->real = p * mean->real + q * num.real;
    mean->complex = mean->complex || num.complex;
    if (mean->complex) mean->imag = p * mean->imag + q * num.imag;
    (*count)++;
  }
  else {
    x = compounddataseq(x);
    n = seqlen(x);
    for (i = 0; i < n; i++) {
      y = getnextelement(&x, i);
      base_mean(count, mean, y);
    }
  }
}
Exemplo n.º 17
0
LVAL xsgetsmdata(V)
{
  LVAL s1, s2, arg;
  LVAL x, y, xs, ys;
  int n, ns, i, supplied, is_reg;
  double xmin, xmax, *dx, *dxs;

  s1 = xlgaseq();
  s2 = xlgetarg();
  arg = xlgetarg();
  is_reg = ! null(xlgetarg());
  xllastarg();

  if (is_reg && ! seqp(s2))
    xlbadtype(s2);
  if (! seqp(arg) && ! fixp(arg))
    xlbadtype(arg);

  ns = (fixp(arg)) ? getfixnum(arg) : seqlen(arg);
  supplied = (seqp(arg) && ns >= 1) ? TRUE : FALSE;
  if (ns < 1) ns = NS_DEFAULT;

  n = seqlen(s1);
  if (n <= 0)
    xlfail("sequence too short");
  if (is_reg && seqlen(s2) != n)
    xlfail("sequences not the same length");
  
  xlstkcheck(4);
  xlsave(x);
  xlsave(y);
  xlsave(xs);
  xlsave(ys);

  x = gen2linalg(s1, n, 1, s_c_double, FALSE);
  y = is_reg ? gen2linalg(s2, n, 1, s_c_double, FALSE) : NIL;
  xs = supplied ?
    gen2linalg(arg, ns, 1, s_c_double, FALSE) : mktvec(ns, s_c_double);
  ys = mktvec(ns, s_c_double);

  if (! supplied) {
    dx = REDAT(x);
    dxs = REDAT(xs);
    for (xmax = xmin = dx[0], i = 1; i < n; i++) {
      if (dx[i] > xmax) xmax = dx[i];
      if (dx[i] < xmin) xmin = dx[i];
    }
    for (i = 0; i < ns; i++)
      dxs[i] = xmin + (xmax - xmin) * ((double) i) / ((double) (ns - 1));
  }

  xlnumresults = 0;
  xlresults[xlnumresults++] = cvfixnum((FIXTYPE) n);
  xlresults[xlnumresults++] = x;
  xlresults[xlnumresults++] = y;
  xlresults[xlnumresults++] = cvfixnum((FIXTYPE) ns);
  xlresults[xlnumresults++] = xs;
  xlresults[xlnumresults++] = ys;
  xlpopn(4);
  return xlresults[0];
}