Example #1
0
/// Returns the formal charge of the atom. This is equal to the
/// difference between the atom's valence and its expected valence.
/// (i.e. valence() - expectedValence()).
int Atom::formalCharge() const
{
    if(is(Hydrogen) || m_element.isMetal())
        return expectedValence() - valence();
    else
        return valence() - expectedValence();
}
Example #2
0
Vector3F AccCorner::computeNormal() const
{
	if(isOnBoundary())
		return *_centerNormal * (2.f / 3.f) +  _edgeNormals[0] * (1.f / 6.f) +  _edgeNormals[valence() - 1] * (1.f / 6.f);
		
	float e = 4.f;
	float c = 1.f;
	float sum = 0.f;
	Vector3F res;
	res.setZero();
	Vector3F q;
	
	for(int i = 0; i < valence(); i++) {
		q = _edgeNormals[i];
		res += q * e;
		sum += e;
		q = _cornerNormals[i];
		res += q * c;
		sum += c;
	}
	
	sum += valence() * valence();
	res += *_centerNormal * valence() * valence();
	return res / sum;
}
Example #3
0
Vector3F AccCorner::computePosition() const
{
	if(isOnBoundary())
		return *_centerPosition * (2.f / 3.f) +  _edgePositions[0] * (1.f / 6.f) +  _edgePositions[valence() - 1] * (1.f / 6.f);
		
	float e = 4.f;
	float c = 1.f;
	float sum = 0.f;
	Vector3F res;
	Vector3F q;
	
	for(int i = 0; i < valence(); i++) {
		q = _edgePositions[i];
		res += q * e;
		sum += e;
		q = _cornerPositions[i];
		res += q * c;
		sum += c;
	}
	
	sum += valence() * valence();
	res += *_centerPosition * valence() * valence();
	res /= sum;
	return res;
}
Example #4
0
nialptr
to_real(nialptr x)
{
  nialptr     z,
              xi;
  nialint     i,
              t = tally(x);
  int         v = valence(x);
  double      r = 0.0;

  /* create the result container */
  z = new_create_array(realtype, v, 0, shpptr(x, v));
  for (i = 0; i < t; i++) {
    int         k;

    xi = fetch_array(x, i);
    k = kind(xi);
    if (k == realtype)
      r = *pfirstreal(xi);

    else  /* must be boolean or integer */ 
    if (k == inttype)
      r = 1.0 * intval(xi);

    else 
    if (k == booltype)
      r = 1.0 * boolval(xi);
    store_real(z, i, r);
  }
  return (z);
}
Example #5
0
bool
Surface_mesh::
is_triangle_mesh() const
{
    Face_iterator fit=faces_begin(), fend=faces_end();
    for (; fit!=fend; ++fit)
        if (valence(fit) != 3)
            return false;

    return true;
}
Example #6
0
// Prints an informative warning if the tree is nonidentifiable.
bool nonident_warning(Tree &T) {
  std::list<long> Lnon;
  std::list<long>::iterator it;
  std::list<long>::iterator itlast;

  for(long i = 0; i < T.nnodes; i++) {
    // Any node (root or not) with valence 2 leads to nonident.
    if(valence(T, i) == 2) {
      Lnon.push_back(i);
    }

    // A root of valence 1 leads to nonident.
    if(valence(T, i) == 1 && i == T.nleaves) {
      Lnon.push_back(i);
    }
  }

  if (Lnon.size() > 0) {
    std::cout << "WARNING: The following nodes lead to non-identifiability of the parameters: ";
    itlast = Lnon.end();
    --itlast;
    for(it=Lnon.begin(); it != Lnon.end(); it++) {
      std::cout << *it;
      if (it != itlast) {     // if not the last element
        std::cout << ", ";
      }
    }
    std::cout << "." << std::endl << std::endl;

    std::cout << "Possible causes:" << std::endl;
    std::cout << " 1) The root has valence 1. If the model has non-uniform distribution, the length of the outgoing edge cannot be recovered reliably." << std::endl;
    std::cout << " 2) There is a node (typically thought of as the root) with exactly two incident edges. In this case only the sum of lengths of the two incident edges can be recovered reliably." << std::endl << std::endl;
  }

  return Lnon.size() > 0;
}
Example #7
0
nialptr
bool_to_real(nialptr x)
{
  nialint     i,
              t = tally(x);
  nialptr     z;
  double     *pz;
  int         v = valence(x);

  z = new_create_array(realtype, v, 0, shpptr(x, v));
  pz = pfirstreal(z);        /* safe */
  for (i = 0; i < t; i++)
    *pz++ = 1.0 * fetch_bool(x, i);
  freeup(x);
  return (z);
}
Example #8
0
int
convert(nialptr * x, int *kx, int k)
{
  if (numeric(k)) {
    nialptr     z = Null;
    int         v = valence(*x);
    nialint     i,
                t = tally(*x);

    switch (k) {
      case inttype:          /* must be converting a booltype array */
          {
            nialint    *zptr;

            z = new_create_array(inttype, v, 0, shpptr(*x, v));
            zptr = pfirstint(z);  /* safe */
            for (i = 0; i < t; i++)
              *zptr++ = fetch_bool(*x, i);
          }
          break; 

      case realtype: /* converting a boolean or integer array */
          {
            double     *zptr;

            z = new_create_array(realtype, v, 0, shpptr(*x, v));
            zptr = pfirstreal(z); /* safe */
            if (*kx == booltype) {
              for (i = 0; i < t; i++)
                *zptr++ = (double) fetch_bool(*x, i);
            }
            else {
              nialint    *xptr = pfirstint(*x); /* safe */

              for (i = 0; i < t; i++)
                *zptr++ = (double) *xptr++;
            }
          }
          break;
    }
    freeup(*x);
    *x = z;
    *kx = kind(z);
    return true;
  }
  return false;
}
Example #9
0
nialptr
int_to_real(nialptr x)
{
  nialint     i,
             *px,
              t = tally(x);
  nialptr     z;
  double     *pz;
  int         v = valence(x);

  z = new_create_array(realtype, v, 0, shpptr(x, v));
  px = pfirstint(x);         /* safe */
  pz = pfirstreal(z);        /* safe */
  for (i = 0; i < t; i++)
    *pz++ = 1.0 * *px++;
  freeup(x);
  return (z);
}
Example #10
0
nialptr
boolstoints(nialptr x)
{
  nialptr     z;
  nialint     i,
              t = tally(x),
             *ptrz;
  int         v = valence(x);

  z = new_create_array(inttype, v, 0, shpptr(x, v));
  ptrz = pfirstint(z);       /* safe */
  for (i = 0; i < t; i++) {
    int         xi;

    xi = fetch_bool(x, i);
    *ptrz++ = xi;
  }
  return (z);
}
Example #11
0
static      nialptr
to_int(nialptr x)
{
  nialptr     z,
              xi;
  nialint     i,
              t = tally(x);
  int         v = valence(x);

  /* create the result container */
  z = new_create_array(inttype, v, 0, shpptr(x, v));
  for (i = 0; i < t; i++) {
    xi = fetch_array(x, i);
    if (kind(xi) == inttype) {
      copy1(z, i, xi, 0);
    }
    else   /* type must be boolean */
      store_int(z, i, boolval(xi));
  }
  return (z);
}
Example #12
0
void
iinnerproduct()
{
  nialptr     z,
              a,
              b,
              x;
  int         va,
              vb,
              vx,
              replicatea,
              replicateb;
  nialint     m,
              n,
              bn,
              p,
              i,
              j,
              k,
              sh[2];

  double      sum,
             *ap,
             *bp,
             *xp;

  z = apop();
  if (tally(z) != 2) {
    buildfault("arg to innerproduct not a pair");
    freeup(z);
    return;
  }
  splitfb(z, &a, &b);
  va = valence(a);
  vb = valence(b);
  if (va > 2 || vb > 2) {
    buildfault("incorrect valence in innerproduct");
    freeup(a);
    freeup(b);
    freeup(z);
    return;
  }
  /* ensure b is realtype */
  switch (kind(b)) {
    case booltype:
        b = bool_to_real(b);
        break;
    case inttype:
        b = int_to_real(b);
        break;
    case realtype:
        break;
    case chartype:
    case phrasetype:
    case faulttype:
        {
          buildfault("second arg not numeric type in innerproduct");
          freeup(a);
          freeup(b);
          freeup(z);
          return;
        }
    case atype:
        {
          nialint     i = 0;

          if (!simple(b)) {
            buildfault("arg not simple in innerproduct");
            freeup(a);
            freeup(b);
            freeup(z);
            return;
          }
          while (i < tally(b)) {
            if (!numeric(kind(fetch_array(b, i)))) {
              buildfault("second arg not numeric type in innerproduct");
              freeup(a);
              freeup(b);
              freeup(z);
              return;
            }
            i++;
          }
          b = to_real(b);    /* safe because freeup(z) will clear old b */
        }
  }
  /* ensure a is realtype */
  switch (kind(a)) {
    case booltype:
        a = bool_to_real(a);
        break;
    case inttype:
        a = int_to_real(a);
        break;
    case realtype:
        break;
    case chartype:
    case phrasetype:
    case faulttype:
        {
          buildfault("first arg not numeric type in innerproduct");
          freeup(a);
          freeup(b);
          freeup(z);
          return;
        }
    case atype:
        {
          nialint     i = 0;

          if (!simple(a)) {
            buildfault("first arg not simple in innerproduct");
            freeup(a);
            freeup(b);
            freeup(z);
            return;
          }
          while (i < tally(a)) {
            if (!numeric(kind(fetch_array(a, i)))) {
              buildfault("first arg not numeric type in innerproduct");
              freeup(a);
              freeup(b);
              freeup(z);
              return;
            }
            i++;
          }
          a = to_real(a);    /* safe becasue freeup of z clears old a */
        }
  }


  if (va == 0) {
    m = 1;
    n = 1;
  }
  else if (va == 1) {
    m = 1;
    n = pickshape(a, 0);
  }
  else {
    m = pickshape(a, 0);
    n = pickshape(a, 1);
  }

  if (vb == 0) {
    bn = 1;
    p = 1;
  }
  else if (vb == 1) {
    bn = pickshape(b, 0);
    p = 1;
  }
  else {
    bn = pickshape(b, 0);
    p = pickshape(b, 1);
  }

  replicatea = (va == 0);
  replicateb = (vb == 0);

  if (!(replicatea || replicateb) && n != bn) {
    buildfault("conform error in innerproduct");
    freeup(a);
    freeup(b);
    freeup(z);
    return;
  }

  /* get valence for the result */
  vx = (va <= 1 ? (vb <= 1 ? 0 : 1) : (vb <= 1 ? 1 : 2));
  if (vx == 2) {
    sh[0] = m;
    sh[1] = p;
  }
  else if (vx == 1)
    sh[0] = (va == 2 ? m : p);
  /* if vx==0 then sh is not used */

  /* allocate space for the result matrix */
  x = new_create_array(realtype, vx, 0, sh);

  ap = pfirstreal(a);        /* safe: no allocations */
  bp = pfirstreal(b);        /* safe: no allocations */
  xp = pfirstreal(x);        /* safe: no allocations */

  /* type of loop chosen on the kind of ip being done */

  if (vx == 2) {             /* matrix - matrix */
    for (i = 0; i < m; i++) {
      for (j = 0; j < p; j++) {
        sum = 0.;
        for (k = 0; k < n; k++)
          sum += *(ap + (n * i + k)) * *(bp + (p * k + j));
        *(xp + (p * i + j)) = sum;
      }
      checksignal(NC_CS_NORMAL);
    }
  }
  else if (va == 2) {        /* matrix - vector */
    for (i = 0; i < m; i++) {
      sum = 0.;
      if (replicateb)
        for (k = 0; k < n; k++)
          sum += *(ap + (n * i + k)) * *bp;
      else
        for (k = 0; k < n; k++)
          sum += *(ap + (n * i + k)) * *(bp + k);
      *(xp + i) = sum;
    }
  }
  else if (vb == 2) {        /* vector - matrix */
    for (j = 0; j < p; j++) {
      sum = 0.;
      if (replicatea)
        for (k = 0; k < bn; k++)
          sum += *ap * *(bp + (p * k + j));
      else
        for (k = 0; k < bn; k++)
          sum += *(ap + k) * *(bp + (p * k + j));
      *(xp + j) = sum;
    }
  }
  else {                     /* vector - vector */
    sum = 0.;
    if (replicatea)
      for (k = 0; k < bn; k++)
        sum += *ap * *(bp + k);
    else if (replicateb)
      for (k = 0; k < n; k++)
        sum += *(ap + k) * *bp;
    else
      for (k = 0; k < n; k++)
        sum += *(ap + k) * *(bp + k);
    *xp = sum;
  }

  apush(x);
  freeup(a);
  freeup(b);
  freeup(z);
}
Example #13
0
void
isolve()
{
  nialptr     z,
              a,
              aa = Null,
              b,
              x = Null;
  int         va,
              vb,
              afreed,
              bfreed;
  nialint     n,
              nrhs;
  char        errmsg[80];

  z = apop();
  if (tally(z) != 2) {
    buildfault("arg to solve not a pair");
    freeup(z);
    return;
  }
  splitfb(z, &a, &b);
  va = valence(a);
  vb = valence(b);
  if (va != 2 || vb > 2) {
    buildfault("incorrect valence in solve");
    freeup(a);
    freeup(b);
    freeup(z);
    return;
  }
  n = pickshape(a, 0);
  if (n != pickshape(a, 1) || n != pickshape(b, 0)) {
    buildfault("shapes do not conform in solve");
    freeup(a);
    freeup(b);
    freeup(z);
    return;
  }
  bfreed = false;
  switch (kind(b)) {
    case booltype:
        x = bool_to_real(b);
        bfreed = true;
        break;
    case inttype:
        x = int_to_real(b);
        bfreed = true;
        break;
    case realtype:
        { /* make a copy of b in x */
          x = new_create_array(realtype, vb, 0, shpptr(b, vb));
          copy(x, 0, b, 0, tally(b));
          break;
        }
    case chartype:
    case phrasetype:
    case faulttype:
        {
          buildfault("second arg not numeric type in solve");
          freeup(a);
          freeup(b);
          freeup(z);
          return;
        }
    case atype:
        {
          nialint     i = 0;

          if (!simple(b)) {
            buildfault("second arg not simple in solve");
            freeup(a);
            freeup(b);
            freeup(z);
            return;
          }
          while (i < tally(b)) {
            if (!numeric(kind(fetch_array(b, i)))) {
              buildfault("second arg not numeric type in solve");
              freeup(a);
              freeup(b);
              freeup(z);
              return;
            }
            i++;
          }
          x = to_real(b);  /* makes a copy of b as reals */
        }
  }
  afreed = false;
  switch (kind(a)) {
    case booltype:
        aa = bool_to_real(a); /* makes a copy of a as reals */
        afreed = true;
        break;
    case inttype:
        aa = int_to_real(a); /* makes a copy of a as reals */
        afreed = true;
        break;
    case realtype:
        { /* makes a copy of a */
          aa = new_create_array(realtype, va, 0, shpptr(a, va));
          copy(aa, 0, a, 0, tally(a));
          break;
        }
    case chartype:
    case phrasetype:
    case faulttype:
        {
          buildfault("first arg not numeric type in solve");
          freeup(a);
          if (!bfreed)
            freeup(b);
          freeup(z);
          freeup(x);
          return;
        }
    case atype:
        {
          nialint     i = 0;

          if (!simple(a)) {
            buildfault("first arg not simple in solve");
            freeup(a);
            if (!bfreed)
              freeup(b);
            freeup(z);
            freeup(x);
            return;
          }
          while (i < tally(a)) {
            if (!numeric(kind(fetch_array(a, i)))) {
              buildfault("first arg not numeric type in solve");
              freeup(a);
              if (!bfreed)
                freeup(b);
              freeup(z);
              freeup(x);
              return;
            }
            i++;
          }
          aa = to_real(a); /* makes a copy of a as reals */
        }
  }


  nrhs = (vb == 1 ? 1 : pickshape(x, 1));

  /* use the Gaussian elimination algorithm to solve the equation(s) */
  if (Gausselim(pfirstreal(aa), pfirstreal(x), n, nrhs, errmsg)) {
    apush(x);
  }
  else {
    buildfault(errmsg);
    freeup(x);
  }
  freeup(aa);                /* the modified copy of a has to be freed */
  if (!bfreed)
    freeup(b);
  if (!afreed)
    freeup(a);
  freeup(z);
}
Example #14
0
void
iinverse()
{
  nialptr     a,
              aa = Null,
              x;
  int         va,
              afreed;
  nialint     n,
              i,
              j;
  double     *ptr;
  char        errmsg[80];

  a = apop();
  va = valence(a);
  if (va != 2) {
    buildfault("incorrect valence in inverse");
    freeup(a);
    return;
  }
  n = pickshape(a, 0);
  if (n != pickshape(a, 1)) {
    buildfault("matrix is not square in inverse");
    freeup(a);
    return;
  }
  afreed = false;
  switch (kind(a)) {
    case booltype:
        aa = bool_to_real(a);  /* makes a real copy of a */
        afreed = true;
        break;
    case inttype:
        aa = int_to_real(a);  /* makes a real copy of a */

        afreed = true;
        break;
    case realtype:
        {  /* make a real copy of a */
          aa = new_create_array(realtype, va, 0, shpptr(a, va));
          copy(aa, 0, a, 0, tally(a));
          break;
        }
    case chartype:
    case phrasetype:
    case faulttype:
        {
          buildfault("arg not numeric type in inverse");
          freeup(a);
          return;
        }
    case atype:
        {
          nialint     i = 0;

          if (!simple(a)) {
            buildfault("arg not simple in inverse");
            freeup(a);
            return;
          }
          while (i < tally(a)) {
            if (!numeric(kind(fetch_array(a, i)))) {
              buildfault("arg not numeric type in inverse");
              freeup(a);
              return;
            }
            i++;
          }
          aa = to_real(a);  /* makes a real copy of a */

        }
  }
  /* initialize x to a real identity matrix */
  x = new_create_array(realtype, va, 0, shpptr(aa, va));
  ptr = pfirstreal(x);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      *ptr++ = (i == j ? 1.0 : 0.0);

  if (Gausselim(pfirstreal(aa), pfirstreal(x), n, n, errmsg)) {
    apush(x);
  }
  else {
    buildfault(errmsg);
    freeup(x);
  }
  freeup(aa);                /* the modified copy of a has to be freed */
  /* if a direct copies of a was made, then it needs to be freed up. the
   * conversion routine does its own freeup. */
  if (!afreed)
    freeup(a);
}