home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.lbl.gov
/
2014.05.ftp.ee.lbl.gov.tar
/
ftp.ee.lbl.gov
/
bmd-1.0beta.tar.Z
/
bmd-1.0beta.tar
/
bmd-1.0beta
/
app
/
omtd
/
pt.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-01-13
|
3KB
|
135 lines
/*
* Copyright (c) 1990 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Lawrence Berkeley Laboratory,
* Berkeley, CA. The name of the University may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* Piece-wise continuous functions.
*/
#include <sys/types.h>
struct pt_list {
u_long x;
float y;
struct pt_list *next;
};
pt_insert(p, x, y)
struct pt_list **p;
u_long x;
float y;
{
struct pt_list *n = (struct pt_list *)malloc(sizeof(*n));
n->x = x;
n->y = y;
while (*p && x > (*p)->x)
p = &(*p)->next;
if (*p && (*p)->x == x) {
free((char *)n);
return;
}
n->next = *p;
*p = n;
}
u_long
pt_integrate(p, x0)
struct pt_list *p;
u_long x0;
{
struct pt_list *n;
float sum = 0.0, h;
while ((n = p->next) && x0 > n->x) {
sum += (p->y + n->y) * (n->x - p->x) / 2.0;
p = n;
}
if (n == 0 || p->y == n->y)
return sum + p->y * (x0 - p->x);
else {
h = p->y + (x0 - p->x) * (n->y - p->y) / (n->x - p->x);
return sum + 0.5 * (p->y + h) * (x0 - p->x);
}
}
double sqrt();
#define SQRT(x) (sqrt((double)(x)))
u_long
pt_back_integrate(p, A)
struct pt_list *p;
u_long A;
{
struct pt_list *n;
float sum = A, a, b;
while (n = p->next) {
a = (p->y + n->y) * (n->x - p->x) / 2.0;
if (sum < a)
break;
sum -= a;
p = n;
}
if (n == 0 || p->y == n->y)
return p->x + (sum / p->y);
/* Solve quadratic. */
a = 0.5 * (n->y - p->y) / (n->x - p->x);
b = p->y;
return p->x + (SQRT(b * b + 4.0 * a * sum) - b) * 0.5 / a;
}
pt_free(p)
struct pt_list *p;
{
while (p) {
struct pt_list *next = p->next;
free((char *)p);
p = next;
}
}
write_ptlist(fd, list)
int fd;
struct pt_list *list;
{
struct pt_list *p;
int cnt = 0;
for (p = list; p != 0; p = p->next)
++cnt;
(void)write(fd, &cnt, sizeof cnt);
for (p = list; p != 0; p = p->next)
(void)write(fd, (char *)p, sizeof *p - sizeof p->next);
}
read_ptlist(fd, listp)
int fd;
struct pt_list **listp;
{
struct pt_list *p;
int cnt = 0;
(void)read(fd, &cnt, sizeof cnt);
while (--cnt >= 0) {
p = (struct pt_list *)malloc(sizeof(*p));
(void)read(fd, (char *)p, sizeof *p - sizeof p->next);
*listp = p;
listp = &p->next;
}
*listp = 0;
}