1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00

Initial checkin

This commit is contained in:
Brian Fiete 2019-08-23 11:56:54 -07:00
parent c74712dad9
commit 078564ac9e
3242 changed files with 1616395 additions and 0 deletions

View file

@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "Common.h"
static void fannkuch(int n, int& sumOut, int& maxflipsOut)
{
int sum = 0;
int maxflips = 0;
/*int* p = (int*)alloca(sizeof(int)*n);
int* q = (int*)alloca(sizeof(int)*n);
int* s = (int*)alloca(sizeof(int)*n);*/
int* p = new int[n];
int* q = new int[n];
int* s = new int[n];
int sign = 1, m = n - 1;
for (int i = 0; i < n; i++) { p[i] = i; q[i] = i; s[i] = i; }
do
{
// Copy and flip.
int q0 = p[0]; // Cache 0th element.
if (q0 != 0)
{
for (int i = 1; i < n; i++) q[i] = p[i]; // Work on a copy.
int flips = 1;
do
{
int qq = q[q0];
if (qq == 0)
{ // ... until 0th element is 0.
sum += sign * flips;
if (flips > maxflips) maxflips = flips; // New maximum?
break;
}
q[q0] = q0;
if (q0 >= 3)
{
int i = 1, j = q0 - 1, t;
do { t = q[i]; q[i] = q[j]; q[j] = t; i++; j--; } while (i < j);
}
q0 = qq; flips++;
} while (true);
}
// Permute.
if (sign == 1)
{
int t = p[1]; p[1] = p[0]; p[0] = t; sign = -1; // Rotate 0<-1.
}
else
{
int t = p[1]; p[1] = p[2]; p[2] = t; sign = 1; // Rotate 0<-1 and 0<-1<-2.
for (int i = 2; i < n; i++)
{
int sx = s[i];
if (sx != 0) { s[i] = sx - 1; break; }
if (i == m)
{
sumOut = sum;
maxflipsOut = maxflips;
return; // Out of permutations.
}
s[i] = i;
// Rotate 0<-...<-i+1.
t = p[0]; for (int j = 0; j <= i; j++) { p[j] = p[j + 1]; } p[i + 1] = t;
}
}
} while (true);
}
void FannkuchRedux(int n)
{
int sum;
int maxflips;
fannkuch(n, sum, maxflips);
Beefy::OutputDebugStrF("%d\nPfannkuchenC(%d) = %d\n", sum, n, maxflips);
}

View file

@ -0,0 +1,140 @@
#pragma warning(disable:4838)
#pragma warning(disable:4305)
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define MIN(x, y) ((x < y) ? x : y)
#define LINELEN 60
#define SLOTS 4095
struct aminoacid {
char c;
float p;
};
static struct aminoacid *lu[SLOTS + 1];
static void repeat_fasta(const char *alu, size_t n)
{
const size_t alulen = strlen(alu);
assert(alulen < 1024);
char buf[1024 + LINELEN];
size_t pos = 0, bytes;
memcpy(buf, alu, alulen);
memcpy(buf + alulen, alu, LINELEN);
while (n) {
bytes = MIN(LINELEN, n);
//fwrite_unlocked(buf + pos, bytes, 1, stdout);
//putchar_unlocked('\n');
pos += bytes;
if (pos > alulen)
pos -= alulen;
n -= bytes;
}
}
static void acc_probs(struct aminoacid *table)
{
struct aminoacid *iter = table;
while ((++iter)->c) {
iter->p += (iter - 1)->p;
}
for (int i = 0; i <= SLOTS; ++i) {
while (i > (table->p * SLOTS))
++table;
lu[i] = table;
}
}
static float rng(float max)
{
const unsigned int IM = 139968, IA = 3877, IC = 29573;
static unsigned int seed = 42;
seed = (seed * IA + IC) % IM;
return max * seed / IM;
}
static char nextc()
{
float r;
struct aminoacid *iter;
r = rng(1.0f);
iter = lu[(int) (r * SLOTS)];
while (iter->p < r)
++iter;
return iter->c;
}
static void random_fasta(struct aminoacid *table, size_t n)
{
size_t i, lines = n / LINELEN;
const size_t chars_left = n % LINELEN;
char buf[LINELEN + 1];
while (lines--) {
for (i = 0; i < LINELEN; ++i) {
buf[i] = nextc();
}
buf[i] = '\n';
//fwrite_unlocked(buf, i + 1, 1, stdout);
}
for (i = 0; i < chars_left; ++i)
buf[i] = nextc();
//fwrite_unlocked(buf, i, 1, stdout);
}
void FastaRedux(int n)
{
//const size_t n = (argc > 1) ? atoi(argv[1]) : 1000;
const char alu [] =
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTG"
"GGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGA"
"GACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAA"
"AATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAAT"
"CCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAAC"
"CCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTG"
"CACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
struct aminoacid iub [] = {
{ 'a', 0.27 },
{ 'c', 0.12 },
{ 'g', 0.12 },
{ 't', 0.27 },
{ 'B', 0.02 },
{ 'D', 0.02 },
{ 'H', 0.02 },
{ 'K', 0.02 },
{ 'M', 0.02 },
{ 'N', 0.02 },
{ 'R', 0.02 },
{ 'S', 0.02 },
{ 'V', 0.02 },
{ 'W', 0.02 },
{ 'Y', 0.02 },
{ 0, 0 }
};
struct aminoacid homosapiens [] = {
{ 'a', 0.3029549426680 },
{ 'c', 0.1979883004921 },
{ 'g', 0.1975473066391 },
{ 't', 0.3015094502008 },
{ 0, 0 }
};
//fputs_unlocked(">ONE Homo sapiens alu\n", stdout);
repeat_fasta(alu, n * 2);
//fputs_unlocked(">TWO IUB ambiguity codes\n", stdout);
acc_probs(iub);
random_fasta(iub, n * 3);
//fputs_unlocked(">THREE Homo sapiens frequency\n", stdout);
acc_probs(homosapiens);
random_fasta(homosapiens, n * 5);
//putchar_unlocked('\n');
}

View file

@ -0,0 +1,136 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "Common.h"
#define pi 3.141592653589793
#define solar_mass (4 * pi * pi)
#define days_per_year 365.24
struct planet {
double x, y, z;
double vx, vy, vz;
double mass;
};
void advance(int nbodies, struct planet * bodies, double dt)
{
int i, j;
for (i = 0; i < nbodies; i++) {
struct planet * b = &(bodies[i]);
for (j = i + 1; j < nbodies; j++) {
struct planet * b2 = &(bodies[j]);
double dx = b->x - b2->x;
double dy = b->y - b2->y;
double dz = b->z - b2->z;
double distance = sqrt(dx * dx + dy * dy + dz * dz);
double mag = dt / (distance * distance * distance);
b->vx -= dx * b2->mass * mag;
b->vy -= dy * b2->mass * mag;
b->vz -= dz * b2->mass * mag;
b2->vx += dx * b->mass * mag;
b2->vy += dy * b->mass * mag;
b2->vz += dz * b->mass * mag;
}
}
for (i = 0; i < nbodies; i++) {
struct planet * b = &(bodies[i]);
b->x += dt * b->vx;
b->y += dt * b->vy;
b->z += dt * b->vz;
}
}
double energy(int nbodies, struct planet * bodies)
{
double e;
int i, j;
e = 0.0;
for (i = 0; i < nbodies; i++) {
struct planet * b = &(bodies[i]);
e += 0.5 * b->mass * (b->vx * b->vx + b->vy * b->vy + b->vz * b->vz);
for (j = i + 1; j < nbodies; j++) {
struct planet * b2 = &(bodies[j]);
double dx = b->x - b2->x;
double dy = b->y - b2->y;
double dz = b->z - b2->z;
double distance = sqrt(dx * dx + dy * dy + dz * dz);
e -= (b->mass * b2->mass) / distance;
}
}
return e;
}
void offset_momentum(int nbodies, struct planet * bodies)
{
double px = 0.0, py = 0.0, pz = 0.0;
int i;
for (i = 0; i < nbodies; i++) {
px += bodies[i].vx * bodies[i].mass;
py += bodies[i].vy * bodies[i].mass;
pz += bodies[i].vz * bodies[i].mass;
}
bodies[0].vx = -px / solar_mass;
bodies[0].vy = -py / solar_mass;
bodies[0].vz = -pz / solar_mass;
}
#define NBODIES 5
struct planet orig_bodies[NBODIES] = {
{ /* sun */
0, 0, 0, 0, 0, 0, solar_mass
},
{ /* jupiter */
4.84143144246472090e+00,
-1.16032004402742839e+00,
-1.03622044471123109e-01,
1.66007664274403694e-03 * days_per_year,
7.69901118419740425e-03 * days_per_year,
-6.90460016972063023e-05 * days_per_year,
9.54791938424326609e-04 * solar_mass
},
{ /* saturn */
8.34336671824457987e+00,
4.12479856412430479e+00,
-4.03523417114321381e-01,
-2.76742510726862411e-03 * days_per_year,
4.99852801234917238e-03 * days_per_year,
2.30417297573763929e-05 * days_per_year,
2.85885980666130812e-04 * solar_mass
},
{ /* uranus */
1.28943695621391310e+01,
-1.51111514016986312e+01,
-2.23307578892655734e-01,
2.96460137564761618e-03 * days_per_year,
2.37847173959480950e-03 * days_per_year,
-2.96589568540237556e-05 * days_per_year,
4.36624404335156298e-05 * solar_mass
},
{ /* neptune */
1.53796971148509165e+01,
-2.59193146099879641e+01,
1.79258772950371181e-01,
2.68067772490389322e-03 * days_per_year,
1.62824170038242295e-03 * days_per_year,
-9.51592254519715870e-05 * days_per_year,
5.15138902046611451e-05 * solar_mass
}
};
struct planet bodies[NBODIES];
void NBody(int n)
{
int i;
memcpy(bodies, orig_bodies, sizeof(orig_bodies));
offset_momentum(NBODIES, bodies);
printf("%.9f\n", energy(NBODIES, bodies));
for (i = 1; i <= n; i++)
advance(NBODIES, bodies, 0.01);
printf("%.9f\n", energy(NBODIES, bodies));
}