[comp.sys.amiga.programmer] Please convert some Basic->C for me.

mcmahan@netcom.COM (Dave Mc Mahan) (06/25/91)

 In a previous article, baxter_a@wehi.dn.mu.oz writes:
>Could someone please help me with a conversion from Basic -> C. I thought
>I knew a bit of Basic, but all these appended ampersands are a bit beyond
>me!

The ampersands are used to explicitly define a variable as a 32 bit integer.
This is talked about in detail in the AmigaBASIC manual I got with my A2000.
The page reference is 8-10.  This is usually an attempt at squeezing a bit of
speed out of a section of code, since it is faster to work with integers than
it is to work with floating point numbers, which are the default type if you
don't explicitly force some other convention.


>Below is the program. The bit I need most is the proceedure lsq,
>and an explanation of the arguents.

It looks like the code is trying to do a least-squares fitting of the data.
Why does it want to do this?  I don't know.  I'm not really wild about 
doing a conversion of code that is as long as you posted, but I'd be happy
to answer some specific questions about what is going on and how you can
re-code it yourself.  It looks like the code is trying to do some curve 
fitting.  It wants to come up with the coefficients to for a polynomial that
can be used to re-create the entire curve based upon data it is passed in a 
file.

Good luck, and send me any questions you have on a particular implentation
problem.  I'd be happy to give you my input.  Make sure to send code segments
you are in doubt about, as I'm not going to keep this file around.

    -dave

-- 
Dave McMahan                            mcmahan@netcom.com
					{apple,amdahl,claris}!netcom!mcmahan

sie@fulcrum.bt.co.uk (Sie) (06/26/91)

I tried to email this but it bounced !!!

              O /
---------------X-------------- cut here --------------------------------------
              O \
/* -*-c-*-
 *
 * Module: ls.c
 *
 * Description: converted from a BASIC program so forgive the globals !!
 *
 * Author: Simon Raybould       (sie@fulcrum.bt.co.uk)
 *
 * Date: Tue 25 Jun 1991
 *
 * Modifications
 * =============
 *
 * $Log$
 *
 */

static char *rcsid = "$Header$";

#include <stdio.h>
#include <fcntl.h>

/*
 * Globals.
 * BASIC unfortunatly relys on globals so until someone re-codes this,
 * they will have to stay. It is not my normal practise to have globals
 * lying around.
 */

int ccrows, cccols;		/* rows and cols in cc array */
int prows, pcols;		/* row and cols in p array */
char *cc, *p, *malloc();

#ifndef ABS
#define ABS(x)  (((x)<0)?-(x):(x))
#endif /* ABS */


main()
{
  char flnstr[BUFSIZ], sstr[BUFSIZ], npmaxstr[BUFSIZ];
  char buf[BUFSIZ];
  int ntmax, npmax, np=0, x, y, j;
  FILE *ifp;
    
  printf(" LEAST SQUARES FIT OF DATA POINTS TO POLYNOMIAL\n\n");
  printf(" Data file records are: x-value space(s) COMMA  space(s) y-value linefeed\n\n\n");

  printf(" Name (with full path) of data file: ");
  fflush(stdout);
  fgets(flnstr, BUFSIZ, stdin);
  flnstr[strlen(flnstr)-1] = '\0'; /* zap newline on end of name */
  if(!(ifp = fopen(flnstr, "r"))) {
    perror("fopen() failed");
    exit(1);
  }
  while(fgets(sstr, BUFSIZ, ifp)) {
    if(*sstr == '*') {
      /*
       * This allows for Multiplot text headings
       * but not for added autoscript numbers in files saved by Multiplot.
       */
      fputs(sstr, stdout);
    } else
      break;
  }
  printf("\nMaximum number of data points: ");
  fflush(stdout);
  npmax = ReadInt(stdin);
  printf("Maximum number of terms in polynomial (including constant): ");
  fflush(stdout);
  ntmax = ReadInt(stdin);
  /* DIM cc(11,11),p(ntmax&+1,npmax&) */
  prows = ntmax+1;
  pcols = npmax;
  ccrows = 11;
  cccols = 11;
  if(!(cc = malloc(ccrows*cccols))) {
    perror("malloc()");
    exit(1);
  }
  if(!(p = malloc(prows*pcols))) {
    perror("malloc()");
    exit(1);
  }
  while(fgets(buf, BUFSIZ, ifp)) {
    if(GetXY(buf, &x, &y) == -1) {
      fprintf(stderr, "GetXY failed\n");
      exit(1);
    }
    np++;
    /* p(1,np&)=1 */    
    p[1*pcols+np] = 1;
    for(j=2; j<=ntmax; j++)
      /* p(j&,np&)=p(j&-1,np&)*x */
      p[j*pcols+np] = p[(j-1)*pcols+np];
  }
  (void)fclose(ifp);
  if(lsq(np, ntmax ,p) == -1) {
    fprintf(stderr, "lsq() - FAILED\n");
    exit(1);
  }
  printf("NOW RUN MULTIPLOT WITH THE SAME DATA FILE, FIT 4th-order POL.\n");
  printf("AND COMPARE COEFFICIENTS WITH THESE 5-TERM ONES.\n\n");
  printf("NOTE THE COEFFS., THEN PRESS RETURN TO QUIT\n");
  fgets(buf, BUFSIZ, stdin);
}

ReadInt(FILE *fp)
{
  char str[BUFSIZ];
  
  fgets(str, BUFSIZ, stdin);
  return atoi(str);
}

GetXY(char *buf, int *xptr, int *yptr)
{
  if(!buf)
    return -1;
  *xptr = atoi(strtok(buf, ","));
  *yptr = atoi(strtok(NULL, ","));
  return 0;
}

lsq(int np, int ntmax)
{
  int k, j, m, nk, piv, z;
  int srows, scols;
  char *s, *malloc();

  /* DIM s(11,11) */
  scols = 11;
  srows = 11;
  if(!(s = malloc(scols*srows))) {
    perror("malloc() failed");
    return -1;
  }
  nk = ntmax+1;
  for(j=1; j<=nk; j++) {
    for(k=1; k<=nk; k++) {
      s[j*scols+k] = 0;
      for(m=1; m<=np; m++) {
	/* s(j&,k&)=s(j&,k&)+w(j&,m&)*w(k&,m&) */
	s[j*scols+k] += p[j*pcols+m]*p[k*pcols+m];
      }
    }
  }
  for(k=1; k<=ntmax; k++) {
    piv = s[k*scols+k];
    for(m=k+1; m<=nk; m++) {
      /* s(k&,m&)=s(k&,m&)/piv */
      s[k*scols+m] /= piv;
    }
    for(j=1; j<=nk; j++) {
      if(j != k) {
	for(m=k+1; m<=nk; m++)
	  s[j*scols+m] -= s[j*scols+k]+s[k*scols+m];
      }
    }
    if(k>1) {
      printf("      For %d terms coefficients are:\n", k);
      for(m=1; m<=k; m++) {
	cc[k*cccols+m] = s[m*scols+nk];
	printf("      %d\n", cc[k*cccols+m]);
      }
      z = sqrt(ABS(s[nk*scols+nk]/(np-k)));
      cc[k*cccols+nk] = z;
      printf("     S.D. = %1.2d\n\n",z);
    }
  }
  return 0;
}
              O /
---------------X-------------- cut here --------------------------------------
              O \

--
Simon J Raybould    (sie@fulcrum.bt.co.uk)            //              {o.o}
"Only joking ... Or am I ?" - Vic Reeves            \X/AMIGA           \-/
===========================================================================
Fulcrum communications L.T.D., Fordrough Lane, Birmingham, B9 5LD, ENGLAND.