/** * This function implements the stability criterion of Mardling, 2008a for coplanar, * three body systems. * @param alle A matrix of _all_ elements in hierarchical coordinates, as * returned by K_getAllElements_jacobi (or supplied by the user). * * @return One of T_INAPPLICABLE, T_STABLE or T_UNSTABLE. The routine returns T_INAPPLICABLE * if one of the following is true: (1) the system contains more of three bodies (returns * T_STABLE if two-body system); (2) the mass conditions for which n:1 resonances dominate * (m_2/m_1 > 0.01 && m_2/m_1 > 0.01 or m_2/m1 > 0.05 || m_3/m_1 > 0.05). The routine returns * T_UNSTABLE if the stability criterion (E_n < 0 and E_(n+1) < 0) is satisfied, * T_STABLE otherwise. */ int K_isMstable_coplanar(const gsl_matrix* alle) { if (MROWS(alle) > 3) return T_INAPPLICABLE; if (MROWS(alle) == 2) return T_STABLE; double m_1 = MSUN_TO_MJUP(MGET(alle, 0, MASS)); double m_2 = MGET(alle, 1, MASS); double m_3 = MGET(alle, 2, MASS); // outside mass criterion if ((m_2 / m_1 < 0.01 || m_3 / m_1 < 0.01) && (m_2 / m_1 < 0.05 && m_3 / m_1 < 0.05)) return T_INAPPLICABLE; // Elements of inner and outer binary double P_i = MGET(alle, 1, PER); double P_o = MGET(alle, 2, PER); double sigma = P_o/P_i; double n = floor(sigma); double E_n = K_E_n(alle, n, sigma); double E_n1 = K_E_n(alle, n+1, sigma); if (E_n < 0 && E_n1 < 0) return T_UNSTABLE; else return T_STABLE; }
OM_uint32 gss_verify_mic(OM_uint32 *minor_status, const gss_ctx_id_t ctx, const gss_buffer_t message_buffer, const gss_buffer_t token_buffer, gss_qop_t *qop_state) { OM_uint32 maj_stat; struct mbuf *m, *mic; if (!ctx) { *minor_status = 0; return (GSS_S_NO_CONTEXT); } MGET(m, M_WAITOK, MT_DATA); if (message_buffer->length > MLEN) MCLGET(m, M_WAITOK); m_append(m, message_buffer->length, message_buffer->value); MGET(mic, M_WAITOK, MT_DATA); if (token_buffer->length > MLEN) MCLGET(mic, M_WAITOK); m_append(mic, token_buffer->length, token_buffer->value); maj_stat = KGSS_VERIFY_MIC(ctx, minor_status, m, mic, qop_state); m_freem(m); m_freem(mic); return (maj_stat); }
/* * smb_mbuf_get * * Allocate mbufs to hold the amount of data specified. * A pointer to the head of the mbuf list is returned. */ struct mbuf * smb_mbuf_get(uchar_t *buf, int nbytes) { struct mbuf *mhead = 0; struct mbuf *m = 0; int count; int offset = 0; while (nbytes) { count = (nbytes > MCLBYTES) ? MCLBYTES : nbytes; nbytes -= count; if (mhead == 0) { MGET(mhead, M_WAIT, MT_DATA); m = mhead; } else { MGET(m->m_next, M_WAIT, MT_DATA); m = m->m_next; } if (count > MLEN) { MCLGET(m, M_WAIT); } m->m_len = count; bcopy(buf + offset, m->m_data, count); offset += count; } return (mhead); }
// This function computes the prices of bermudan swaption corresponding to the exercise strategy defined by andersen_struct. static double AmOption_Price_Andersen(AndersenStruct *andersen_struct) { long NbrMCsimulation; int NbrExerciseDates, j, j_start, m; double andersen_param, discounted_payoff, mean_estim, numeraire_j, PriceBermSwp; Interpolate_AndersenParams(andersen_struct); j_start = andersen_struct->j_start; NbrExerciseDates = andersen_struct->NbrExerciseDates; NbrMCsimulation = andersen_struct->NbrMCsimulation; mean_estim=0.; for (m=0; m<NbrMCsimulation; m++) { j=j_start; do { discounted_payoff = MGET(andersen_struct->DiscountedPayoff, j, m); numeraire_j = MGET(andersen_struct->NumeraireValue, j, m); andersen_param = GET(andersen_struct->AndersenParams, j); j++; } while (discounted_payoff*numeraire_j <=andersen_param && j<NbrExerciseDates); mean_estim += discounted_payoff; } PriceBermSwp = mean_estim/(double)NbrMCsimulation; return PriceBermSwp; }
DMD *getdmd(int drv) { DMD *dm; #if DBGFSDRIVE kprintf("getdmd(%i)\n", drv); #endif if (!(drvtbl[drv] = dm = MGET(DMD))) return ( (DMD *) 0 ); if (!(dm->m_dtl = MGET(DND))) goto fredm; if (!(dm->m_dtl->d_ofd = MGET(OFD))) goto fredtl; if (!(dm->m_fatofd = MGET(OFD))) goto freofd; return(dm); freofd: xmfreblk (dm->m_dtl->d_ofd); fredtl: xmfreblk (dm->m_dtl); fredm: xmfreblk (dm); return ( (DMD *) 0 ); }
/*Moments of normal distribution*/ static int moments_normal(PnlVect *c, int m, double mu, double gamma2) { /* Input: PnlVect *c is of dimension m, mu=mean of normal distribution, gamma2=variance of normal distribution. The moments of the normal distribution with mean mu and variance gamma2 up to degree m are calculated and stored in c. */ int i,j,n,index1; PnlMat *a; index1=(int)(m/2+1.); LET(c,0)=mu; a=pnl_mat_create_from_double(m,index1,0.); for(j=0;j<m;j++) { MLET(a,j,0)=1; } for(i=2;i<m+1;i++) { index1=(int)(i/2+1.); for (n=2;n<=index1;n++) { MLET(a,i-1,n-1)=MGET(a,i-2,n-2)*(i-1-2*(n-1)+2)+MGET(a,i-2,n-1); LET(c,i-1)=GET(c,i-1)+MGET(a,i-1,n-1)*pow(mu, (double) i-2.*(double) n+2.)*pow(sqrt(gamma2), 2*(double)n-2.); } LET(c,i-1)=GET(c,i-1)+ pow (mu,(double)i); } pnl_mat_free (&a); return 1.; }
void mvn_sample(gsl_vector *mean_cand, gsl_matrix *var) { /* Takes a mean vec, mean and var matrix, * var and gives vector of MVN(mean,var) realisations, x */ int i, j; int dimen = var -> size1; double value; gsl_matrix *disp; gsl_vector *ran; gsl_matrix *fast_species; fast_species = gsl_matrix_alloc(2, 2); gsl_matrix_set_identity(fast_species); for(i=0;i<dimen; i++) { if(MGET(var, i, i) <0.00000000001) { MSET(var, i, i, 1.0); MSET(fast_species, i, i, 0.0); } } disp = gsl_matrix_alloc(2, 2); ran = gsl_vector_alloc(2); gsl_matrix_memcpy(disp, var); if(postive_definite == 1) { gsl_linalg_cholesky_decomp(disp); for(i=0;i<dimen;i++) { for (j=i+1;j<dimen;j++) { MSET(disp,i,j,0.0); } } }else{ value = pow(MGET(disp, 0 ,0), 0.5); gsl_matrix_set_identity(disp); MSET(disp, 0,0, value); MSET(disp, 1,1, value); } for (j=0;j<dimen;j++) { VSET(ran,j,gsl_ran_gaussian(r,1.0)); } /*remove update from slow species*/ gsl_matrix_mul_elements(disp, fast_species); /*Add noise to mean cand*/ gsl_blas_dgemv(CblasNoTrans,1.0, disp, ran, 1.0, mean_cand); for(i=0; i<2; i++) { if(VGET(mean_cand,i)<=0.0001 && MGET(fast_species, i, i) > 0.000001) VSET(mean_cand,i,0.0001); } gsl_vector_free(ran); gsl_matrix_free(disp); gsl_matrix_free(fast_species); }
void move(struct mat * ro, struct mat * RO_r, struct mat * RO_n, struct mat * r, struct mat * u, struct mat * n) { double a = MGET(r, 2, 0); double dx = MGET(u, 0, 0) + MGET(n, 0, 0); double da = MGET(u, 1, 0) + MGET(n, 1, 0); double ao = a + da; //printf("ao = %f\n", ao); if (ao > M_PI) { ao = ao - 2.0 * M_PI; } if (ao < -M_PI) { ao = ao + 2.0 * M_PI; } struct mat * dp = mat(2, 1); MSET(dp, 0, 0, dx); //MSET(dp, 1, 0, da); struct mat * to = mat(2, 1); struct mat * TO_r = mat(2, 3); struct mat * TO_dt = mat(2, 2); from_frame(to, TO_r, TO_dt, r, dp); float AO_a = 1; float AO_da = 1; copy_into(RO_r, TO_r, 0, 0); MSET(RO_r, 2, 0, 0); MSET(RO_r, 2, 1, 0); MSET(RO_r, 2, 2, AO_a); copy_into(RO_n, TO_dt, 0, 0); MSET(RO_n, 0, 1, 0); MSET(RO_n, 1, 1, 0); MSET(RO_n, 2, 1, AO_da); //print_mat(to); //printf("%f\n", ao); copy_into(ro, to, 0, 0); MSET(ro, 2, 0, ao); free(to); free(TO_r); free(TO_dt); free(dp); }
/* * Try to reconfigure the audio device to match the file encoding. * If this fails, we should attempt to make the input data match the * device encoding. For now, we give up on this file. * * Returns TRUE if successful. Returns FALSE if not. */ static int reconfig(void) { int err; char msg[AUDIO_MAX_ENCODE_INFO]; Dev_hdr = File_hdr; err = audio_set_play_config(Audio_fd, &Dev_hdr); switch (err) { case AUDIO_SUCCESS: return (TRUE); case AUDIO_ERR_NOEFFECT: /* * Couldn't change the device. * Check to see if we're nearly compatible. * audio_cmp_hdr() returns >0 if only sample rate difference. */ if (audio_cmp_hdr(&Dev_hdr, &File_hdr) > 0) { double ratio; ratio = (double)abs((int) (Dev_hdr.sample_rate - File_hdr.sample_rate)) / (double)File_hdr.sample_rate; if (ratio <= SAMPLE_RATE_THRESHOLD) { if (Verbose) { Error(stderr, MGET("%s: WARNING: %s sampled at " "%d, playing at %d\n"), prog, Ifile, File_hdr.sample_rate, Dev_hdr.sample_rate); } return (TRUE); } Error(stderr, MGET("%s: %s sample rate %d not available\n"), prog, Ifile, File_hdr.sample_rate); return (FALSE); } (void) audio_enc_to_str(&File_hdr, msg); Error(stderr, MGET("%s: %s encoding not available: %s\n"), prog, Ifile, msg); return (FALSE); default: Error(stderr, MGET("%s: %s audio encoding type not available\n"), prog, Ifile); exit(1); } return (TRUE); }
/* * initmux() * * Description: * Allocates memory for carrying out demultiplexing/multiplexing. * * Arguments: * int unitsz Bytes per unit * int unitsp Samples per unit * * Returns: * void */ static void initmux(int unitsz, int unitsp) { int c; /* Channel */ int in_ch_size; /* Input channel size */ /* Size of each input channel */ in_ch_size = insiz / File_hdr.channels; /* Size of each output channel */ out_ch_size = in_ch_size * unitsp / unitsz; /* Allocate pointers to input channels */ in_ch_data = malloc(sizeof (unsigned char *) * File_hdr.channels); if (in_ch_data == NULL) { Error(stderr, MGET("%s: couldn't allocate %dK buf\n"), prog, sizeof (unsigned char *) * File_hdr.channels / 1000); exit(1); } /* Allocate input channels */ for (c = 0; c < File_hdr.channels; c++) { in_ch_data[c] = malloc(sizeof (unsigned char) * in_ch_size); if (in_ch_data[c] == NULL) { Error(stderr, MGET("%s: couldn't allocate %dK buf\n"), prog, in_ch_size / 1000); exit(1); } } /* Allocate pointers to output channels */ out_ch_data = malloc(sizeof (unsigned char *) * File_hdr.channels); if (out_ch_data == NULL) { Error(stderr, MGET("%s: couldn't allocate %dK buf\n"), prog, sizeof (unsigned char *) * File_hdr.channels / 1000); exit(1); } /* Allocate output channels */ for (c = 0; c < File_hdr.channels; c++) { out_ch_data[c] = malloc(sizeof (unsigned char) * out_ch_size); if (out_ch_data[c] == NULL) { Error(stderr, MGET("%s: couldn't allocate %dK buf\n"), prog, out_ch_size / 1000); exit(1); } } }
double K_getPhasedRVLine(ok_kernel* k, int planet, int row, int column) { static gsl_matrix* phasedRVLine = NULL; if (planet >= 1) { if (k->ndata == 0) return -1; int np = K_getNplanets(k); double masses[np + 1]; double periods[np + 1]; for (int i = 1; i <= np; i++) { masses[i] = K_getElement(k, i, MASS); periods[i] = K_getElement(k, i, PER); if (i != planet) { K_setElement(k, i, MASS, 0.); K_setElement(k, i, PER, 10000.); } }; double period = K_getElement(k, planet, PER); int samples = -row; if (phasedRVLine != NULL) { gsl_matrix_free(phasedRVLine); phasedRVLine = NULL; } double** comp = K_getCompiled(k); phasedRVLine = K_integrateStellarVelocity(k, comp[0][0], comp[k->ndata - 1][0], samples, NULL, NULL); double mint = MGET(phasedRVLine, 0, T_TIME); for (int i = 0; i < MROWS(phasedRVLine); i++) { double t = fmod((MGET(phasedRVLine, i, 0) - mint), period); MSET(phasedRVLine, i, 0, t); } ok_sort_matrix(phasedRVLine, 0); for (int i = 1; i <= np; i++) { K_setElement(k, i, MASS, masses[i]); K_setElement(k, i, PER, periods[i]); } return 1; } else { return MGET(phasedRVLine, row, column); } }
/* * Allocate enough mbufs to accommodate the residual count in uio, * and setup the uio_iov to point to them. * * This is used by the various SMB read code paths. That code is * going to do a disk read into this buffer, so we'd like it to be * large and contiguous. Use an external (M_EXT) buffer. */ struct mbuf * smb_mbuf_allocate(struct uio *uio) { mbuf_t *m = 0; int len = uio->uio_resid; MGET(m, M_WAIT, MT_DATA); if (len > MCLBYTES) { /* Like MCLGET(), but bigger buf. */ m->m_ext.ext_buf = kmem_zalloc(len, KM_SLEEP); m->m_data = m->m_ext.ext_buf; m->m_flags |= M_EXT; m->m_ext.ext_size = len; m->m_ext.ext_ref = smb_mbuf_kmem_ref; } else if (len > MLEN) { /* Use the kmem cache. */ MCLGET(m, M_WAIT); } m->m_len = len; uio->uio_iov->iov_base = m->m_data; uio->uio_iov->iov_len = m->m_len; uio->uio_iovcnt = 1; return (m); }
/* Helper routine that appends data, control, and address to a sockbuf. */ static int sbappendaddr_locked_internal(struct sockbuf *sb, const struct sockaddr *asa, struct mbuf *m0, struct mbuf *control, struct mbuf *ctrl_last) { struct mbuf *m, *n, *nlast; #if MSIZE <= 256 if (asa->sa_len > MLEN) return (0); #endif MGET(m, M_DONTWAIT, MT_SONAME); if (m == 0) return (0); m->m_len = asa->sa_len; bcopy(asa, mtod(m, caddr_t), asa->sa_len); if (ctrl_last) ctrl_last->m_next = m0; /* concatenate data to control */ else control = m0; m->m_next = control; for (n = m; n->m_next != NULL; n = n->m_next) sballoc(sb, n); sballoc(sb, n); nlast = n; SBLINKRECORD(sb, m); sb->sb_mbtail = nlast; SBLASTMBUFCHK(sb); SBLASTRECORDCHK(sb); return (1); }
void igmp_init(void) { struct ipoption *ra; /* * To avoid byte-swapping the same value over and over again. */ igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP); igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP); igmp_timers_are_running = 0; /* * Construct a Router Alert option to use in outgoing packets */ MGET(router_alert, M_DONTWAIT, MT_DATA); ra = mtod(router_alert, struct ipoption *); ra->ipopt_dst.s_addr = 0; ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ ra->ipopt_list[1] = 0x04; /* 4 bytes long */ ra->ipopt_list[2] = 0x00; ra->ipopt_list[3] = 0x00; router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1]; Head = (struct router_info *) 0; }
static void usage(void) { Error(stderr, MGET("Record an audio file -- usage:\n" "\t%s [-af] [-v vol]\n" "\t%.*s [-c channels] [-s rate] [-e encoding]\n" "\t%.*s [-t time] [-i info] [-d dev] [-T au|wav|aif[f]] [file]\n" "where:\n" "\t-a\tAppend to output file\n" "\t-f\tIgnore sample rate differences on append\n" "\t-v\tSet record volume (0 - %d)\n" "\t-c\tSpecify number of channels to record\n" "\t-s\tSpecify rate in samples per second\n" "\t-e\tSpecify encoding (ulaw | alaw | [u]linear | linear8 )\n" "\t-t\tSpecify record time (hh:mm:ss.dd)\n" "\t-i\tSpecify a file header information string\n" "\t-d\tSpecify audio device (default: /dev/audio)\n" "\t-T\tSpecify the audio file type (default: au)\n" "\tfile\tRecord to named file\n" "\t\tIf no file specified, write to stdout\n" "\t\tDefault audio encoding is ulaw, 8khz, mono\n" "\t\tIf -t is not specified, record until ^C\n"), prog, strlen(prog), " ", strlen(prog), " ", MAX_GAIN); exit(1); }
/* * Partition an mbuf chain in two pieces, returning the tail -- * all but the first len0 bytes. In case of failure, it returns NULL and * attempts to restore the chain to its original state. */ struct mbuf * m_split(struct mbuf *m0, int len0, int wait) { struct mbuf *m, *n; unsigned len = len0, remain, olen; for (m = m0; m && len > m->m_len; m = m->m_next) len -= m->m_len; if (m == NULL) return (NULL); remain = m->m_len - len; if (m0->m_flags & M_PKTHDR) { MGETHDR(n, wait, m0->m_type); if (n == NULL) return (NULL); if (m_dup_pkthdr(n, m0, wait)) { m_freem(n); return (NULL); } n->m_pkthdr.len -= len0; olen = m0->m_pkthdr.len; m0->m_pkthdr.len = len0; if (m->m_flags & M_EXT) goto extpacket; if (remain > MHLEN) { /* m can't be the lead packet */ MH_ALIGN(n, 0); n->m_next = m_split(m, len, wait); if (n->m_next == NULL) { (void) m_free(n); m0->m_pkthdr.len = olen; return (NULL); } else return (n); } else MH_ALIGN(n, remain); } else if (remain == 0) { n = m->m_next; m->m_next = NULL; return (n); } else { MGET(n, wait, m->m_type); if (n == NULL) return (NULL); M_ALIGN(n, remain); } extpacket: if (m->m_flags & M_EXT) { n->m_ext = m->m_ext; MCLADDREFERENCE(m, n); n->m_data = m->m_data + len; } else { memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain); } n->m_len = remain; m->m_len = len; n->m_next = m->m_next; m->m_next = NULL; return (n); }
/* * Generate the rpc reply header * siz arg. is used to decide if adding a cluster is worthwhile */ struct mbuf * nfs_rephead(int siz, struct nfsrv_descript *nd, int err, struct mbuf **mbp, caddr_t *bposp) { u_int32_t *tl; struct mbuf *mreq; caddr_t bpos; struct mbuf *mb; if (err == EBADRPC) return (NULL); nd->nd_repstat = err; if (err && (nd->nd_flag & ND_NFSV3) == 0) /* XXX recheck */ siz = 0; MGET(mreq, M_WAITOK, MT_DATA); /* * If this is a big reply, use a cluster */ mreq->m_len = 0; if (siz >= MINCLSIZE) { MCLGET(mreq, M_WAITOK); } mb = mreq; bpos = mtod(mb, caddr_t); if (err != NFSERR_RETVOID) { tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED); if (err) *tl = txdr_unsigned(nfsrv_errmap(nd, err)); else *tl = 0; }
OM_uint32 gss_get_mic(OM_uint32 *minor_status, const gss_ctx_id_t ctx, gss_qop_t qop_req, const gss_buffer_t message_buffer, gss_buffer_t message_token) { OM_uint32 maj_stat; struct mbuf *m, *mic; if (!ctx) { *minor_status = 0; return (GSS_S_NO_CONTEXT); } MGET(m, M_WAITOK, MT_DATA); if (message_buffer->length > MLEN) MCLGET(m, M_WAITOK); m_append(m, message_buffer->length, message_buffer->value); maj_stat = KGSS_GET_MIC(ctx, minor_status, qop_req, m, &mic); m_freem(m); if (maj_stat == GSS_S_COMPLETE) { message_token->length = m_length(mic, NULL); message_token->value = malloc(message_token->length, M_GSSAPI, M_WAITOK); m_copydata(mic, 0, message_token->length, message_token->value); m_freem(mic); } return (maj_stat); }
struct mbuf * xdrmbuf_getall(XDR *xdrs) { struct mbuf *m0, *m; KASSERT(xdrs->x_ops == &xdrmbuf_ops && xdrs->x_op == XDR_DECODE, ("xdrmbuf_append: invalid XDR stream")); m0 = (struct mbuf *) xdrs->x_base; m = (struct mbuf *) xdrs->x_private; if (m0 != m) { while (m0->m_next != m) m0 = m0->m_next; m0->m_next = NULL; xdrs->x_private = NULL; } else { xdrs->x_base = NULL; xdrs->x_private = NULL; } if (m) m_adj(m, xdrs->x_handy); else MGET(m, M_WAITOK, MT_DATA); return (m); }
/* * Ensure len bytes of contiguous space at the beginning of the mbuf chain */ struct mbuf * m_prepend(struct mbuf *m, int len, int how) { struct mbuf *mn; if (len > MHLEN) panic("mbuf prepend length too big"); if (M_LEADINGSPACE(m) >= len) { m->m_data -= len; m->m_len += len; } else { MGET(mn, how, m->m_type); if (mn == NULL) { m_freem(m); return (NULL); } if (m->m_flags & M_PKTHDR) M_MOVE_PKTHDR(mn, m); mn->m_next = m; m = mn; MH_ALIGN(m, len); m->m_len = len; } if (m->m_flags & M_PKTHDR) m->m_pkthdr.len += len; return (m); }
/** * Get a summary statistic for the orbital elements; for instance, * the median value calculated over all the elements of the list. * @param kl List * @param what Can be one of: STAT_MEAN, STAT_MEDIAN, STAT_STDDEV, STAT_MAD. * Summary statistic is calculated correctly for angle parameters. * @return A matrix whose entries are the summary statistic for the * corresponding orbital element. */ gsl_matrix* KL_getElementsStats(const ok_list* kl, const int what) { int npl = MROWS(kl->kernels[0]->elements); if (npl == 0) return NULL; gsl_vector* v = gsl_vector_alloc(kl->size); gsl_matrix* m = gsl_matrix_alloc(npl, ALL_ELEMENTS_SIZE); gsl_matrix_set_all(m, 0.); for (int i = 0; i < npl; i++) for (int j = 0; j < ALL_ELEMENTS_SIZE; j++) { for (int n = 0; n < kl->size; n++) { VSET(v, n, MGET(kl->kernels[n]->elements, i, j)); } switch (what) { case STAT_MEAN: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) MSET(m, i, j, ok_average_angle(v->data, v->size, false)); else MSET(m, i, j, gsl_stats_mean(v->data, 1, v->size)); break; case STAT_STDDEV: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) { MSET(m, i, j, ok_stddev_angle(v->data, v->size, false)); } else MSET(m, i, j, gsl_stats_sd(v->data, 1, v->size)); break; case STAT_MEDIAN: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) MSET(m, i, j, ok_median_angle(v->data, v->size, false)); else { gsl_sort_vector(v); MSET(m, i, j, gsl_stats_median_from_sorted_data(v->data, 1, v->size)); } break; case STAT_MAD: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) { double med = ok_median_angle(v->data, v->size, false); MSET(m, i, j, 1.4826 * ok_mad_angle(v->data, v->size, med, false)); } else { gsl_sort_vector(v); double med = gsl_stats_median_from_sorted_data(v->data, 1, v->size); MSET(m, i, j, 1.4826 * ok_mad(v->data, v->size, med)); } break; default: // percentiles gsl_sort_vector(v); MSET(m, i, j, gsl_stats_quantile_from_sorted_data(v->data, 1, v->size, (double)(what)/100.)); }; } gsl_vector_free(v); return m; }
/** * Returns a vector of the orbital element el for planet pl, built from * the list * @param kl * @param pl * @param el * @return */ gsl_vector* KL_getElements(const ok_list* kl, const int pl, const int el) { gsl_vector* v = gsl_vector_alloc(kl->size); for (int i = 0; i < kl->size; i++) VSET(v, i, MGET(kl->kernels[i]->elements, pl, el)); return v; }
/* * m_pullup2() works like m_pullup, save that len can be <= MCLBYTES. * m_pullup2() only works on values of len such that MHLEN < len <= MCLBYTES, * it calls m_pullup() for values <= MHLEN. It also only coagulates the * reqested number of bytes. (For those of us who expect unwieldly option * headers. * * KEBE SAYS: Remember that dtom() calls with data in clusters does not work! */ struct mbuf * m_pullup2(struct mbuf *n, int len) { struct mbuf *m; int count; if (len <= MHLEN) return m_pullup(n, len); if ((n->m_flags & M_EXT) != 0 && n->m_data + len < &n->m_data[MCLBYTES] && n->m_next) { if (n->m_len >= len) return (n); m = n; n = n->m_next; len -= m->m_len; } else { if (len > MCLBYTES) goto bad; MGET(m, M_DONTWAIT, n->m_type); if (m == NULL) goto bad; MCLGET(m, M_DONTWAIT); if ((m->m_flags & M_EXT) == 0) goto bad; m->m_len = 0; if (n->m_flags & M_PKTHDR) { /* Too many adverse side effects. */ /* M_MOVE_PKTHDR(m, n); */ m->m_flags = (n->m_flags & M_COPYFLAGS) | M_EXT | M_CLUSTER; M_MOVE_HDR(m, n); /* n->m_data is cool. */ } } do { count = min(len, n->m_len); bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, (unsigned)count); len -= count; m->m_len += count; n->m_len -= count; if (n->m_len) n->m_data += count; else n = m_free(n); } while (len > 0 && n); if (len > 0) { (void)m_free(m); goto bad; } m->m_next = n; return (m); bad: m_freem(n); MPFail++; return (NULL); }
/* * Help break down an mbuf chain by setting the first siz bytes contiguous * pointed to by returned val. * This is used by the macro NFSM_DISSECT for tough * cases. */ APPLESTATIC void * nfsm_dissct(struct nfsrv_descript *nd, int siz, int how) { mbuf_t mp2; int siz2, xfer; caddr_t p; int left; caddr_t retp; retp = NULL; left = NFSMTOD(nd->nd_md, caddr_t) + mbuf_len(nd->nd_md) - nd->nd_dpos; while (left == 0) { nd->nd_md = mbuf_next(nd->nd_md); if (nd->nd_md == NULL) return (retp); left = mbuf_len(nd->nd_md); nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t); } if (left >= siz) { retp = nd->nd_dpos; nd->nd_dpos += siz; } else if (mbuf_next(nd->nd_md) == NULL) { return (retp); } else if (siz > ncl_mbuf_mhlen) { panic("nfs S too big"); } else { MGET(mp2, MT_DATA, how); if (mp2 == NULL) return (NULL); mbuf_setnext(mp2, mbuf_next(nd->nd_md)); mbuf_setnext(nd->nd_md, mp2); mbuf_setlen(nd->nd_md, mbuf_len(nd->nd_md) - left); nd->nd_md = mp2; retp = p = NFSMTOD(mp2, caddr_t); NFSBCOPY(nd->nd_dpos, p, left); /* Copy what was left */ siz2 = siz - left; p += left; mp2 = mbuf_next(mp2); /* Loop around copying up the siz2 bytes */ while (siz2 > 0) { if (mp2 == NULL) return (NULL); xfer = (siz2 > mbuf_len(mp2)) ? mbuf_len(mp2) : siz2; if (xfer > 0) { NFSBCOPY(NFSMTOD(mp2, caddr_t), p, xfer); NFSM_DATAP(mp2, xfer); mbuf_setlen(mp2, mbuf_len(mp2) - xfer); p += xfer; siz2 -= xfer; } if (siz2 > 0) mp2 = mbuf_next(mp2); } mbuf_setlen(nd->nd_md, siz); nd->nd_md = mp2; nd->nd_dpos = NFSMTOD(mp2, caddr_t); } return (retp); }
static const char *macho_file_needed_lib( fatobj *fobj, char *found_needed, char *found_lib, int op, char **ret, size_t *ret_len ) { loadcmd *lcmd; uint32_t lc_load_dylib; if ((op == 0 && !show_needed) || (op == 1 && !find_lib)) return NULL; lcmd = firstloadcmd(fobj); lc_load_dylib = MGET(fobj->swapped, LC_LOAD_DYLIB); do { if (lcmd->lcmd->cmd == lc_load_dylib) { struct dylib_command *dlcmd = lcmd->data; char *needed; needed = lcmd->data + MGET(fobj->swapped, dlcmd->dylib.name.offset); if (op == 0) { if (!be_wewy_wewy_quiet) { if (*found_needed) xchrcat(ret, ',', ret_len); xstrcat(ret, needed, ret_len); } *found_needed = 1; } else { if (!strncmp(find_lib, needed, strlen(!g_match ? needed : find_lib))) { *found_lib = 1; free(lcmd); return (be_wewy_wewy_quiet ? NULL : needed); } } } } while (nextloadcmd(lcmd)); if (op == 0 && !*found_needed && be_verbose) warn("Mach-O lacks LC_LOAD_DYLIB commands: %s", fobj->filename); return NULL; }
double Barrier::payoff(const PnlMat *path) { PnlVect * lastrow = pnl_vect_new(); pnl_mat_get_row(lastrow, path, TimeSteps_-1); double prod = pnl_vect_scalar_prod(lastrow, payOffCoeff_) - S_; pnl_vect_free(&lastrow); if (std::max(prod,0.0) == 0.0) { return 0.0; } for (int i = 0; i < size_; i++) { for (int j = 0; j < TimeSteps_; j++) { if (MGET(path, j, i) < GET(lowerBarrier_, i) || MGET(path, j, i) > GET(upperBarrier_, i)) { return 0.0; } } } return prod; }
/* Approximation of the call/put function by a polynomial*/ static int pol_approx(NumFunc_1 *p,PnlVect *coeff, double S0, double K, int Nb_Degree_Pol) { /* Input: NumFunc_1 *p specifies the payoff function (Call or Put), PnlVect *coeff is of dimension Nb_Degree_Pol+1, S0 initial value to determine the interval where the approximation is done, K strike, Nb_Degree_Pol corresponds to the degree of the approximating polynomial. The coefficients of the polynomial approximating the Call/Put payoff function are calculated and stored in coeff in increasing order starting with the coefficient corresponding to degree 0. */ PnlMat *x; PnlVect *y; PnlBasis *f; int dim; int j; dim=(int)((log(S0*10)-log(S0/10))/0.01+0.5); /* [log(S0/10), log(S0*10)] is the interval of approximation*/ x=pnl_mat_create_from_double(dim,1,0.); y=pnl_vect_create_from_double(dim,0.); MLET(x,0,0)=log(S0/10); LET(y,0)=(p->Compute)(p->Par,S0/10.); for(j=1;j<dim;j++) { MLET(x,j,0)=MGET(x,j-1,0)+0.01; /* grid of equally spaced points with distance 0.01 in interval [log(S0/10), log(S0*10)] */ LET(y,j)=(p->Compute)(p->Par,exp(MGET(x,j,0))); /* evaluation of the payoff function at x */ } f=pnl_basis_create(PNL_BASIS_CANONICAL,Nb_Degree_Pol+1,1); pnl_basis_fit_ls(f,coeff,x,y); pnl_basis_free (&f); pnl_mat_free(&x); pnl_vect_free(&y); return 1.; }
/* ** getdmd - allocate storage for and initialize a DMD */ static DMD *getdmd(int drv) { DMD *dm; KDEBUG(("getdmd(%i)\n",drv)); if (!(drvtbl[drv] = dm = MGET(DMD))) return NULL; /* * no need to check the following, since MGET(DND) * and MGET(OFD) only return if they succeed */ dm->m_dtl = MGET(DND); dm->m_dtl->d_ofd = MGET(OFD); dm->m_fatofd = MGET(OFD); return dm; }
/* * Pull read data off a interface. * Len is length of data, with local net header stripped. */ static struct mbuf * elget(caddr_t buf, int totlen, struct ifnet *ifp) { struct mbuf *top, **mp, *m; int len; caddr_t cp; char *epkt; cp = buf; epkt = cp + totlen; MGETHDR(m, MB_DONTWAIT, MT_DATA); if (m == 0) return (0); m->m_pkthdr.len = totlen; m->m_len = MHLEN; top = 0; mp = ⊤ while (totlen > 0) { if (top) { MGET(m, MB_DONTWAIT, MT_DATA); if (m == 0) { m_freem(top); return (0); } m->m_len = MLEN; } len = min(totlen, epkt - cp); if (len >= MINCLSIZE) { MCLGET(m, MB_DONTWAIT); if (m->m_flags & M_EXT) m->m_len = len = min(len, MCLBYTES); else len = m->m_len; } else { /* * Place initial small packet/header at end of mbuf. */ if (len < m->m_len) { if (top == 0 && len + max_linkhdr <= m->m_len) m->m_data += max_linkhdr; m->m_len = len; } else len = m->m_len; } bcopy(cp, mtod(m, caddr_t), (unsigned)len); cp += len; *mp = m; mp = &m->m_next; totlen -= len; if (cp == epkt) cp = buf; } return (top); }
/* * xmgetmd - get an MD * * To create a single pool for all osmem requests, MDs are grouped in * blocks of 3 called MDBLOCKs which occupy 58 bytes. MDBLOCKs are * handled as follows: * . they are linked in a chain, initially empty * . when the first MD is required, an MDBLOCK is obtained via * xmgetblk() and put on the chain, and the first slot is allocated * . MDs are obtained from existing partially-used MDBLOCKS * . when an MDBLOCK is full, it is removed from the chain * . when an MD in a full MDBLOCK is freed, the MDBLOCK is put back * on the chain * . when the MDBLOCK is totally unused, it is put back on the normal * free chain */ MD *xmgetmd(void) { MDBLOCK *mdb = mdbroot; MD *md; WORD i, avail; if (!mdb) { mdb = MGET(MDBLOCK); if (!mdb) return NULL; /* initialise new MDBLOCK */ mdb->mdb_next = NULL; for (i = 0; i < MDS_PER_BLOCK; i++) mdb->entry[i].index = -1; /* unused */ mdbroot = mdb; KDEBUG(("xmgetmd(): got new MDBLOCK at %p\n",mdb)); } /* * allocate MD from MDBLOCK */ for (i = 0, avail = 0, md = NULL; i < MDS_PER_BLOCK; i++) { if (mdb->entry[i].index < 0) { if (!md) /* not yet allocated */ { mdb->entry[i].index = i; md = &mdb->entry[i].md; KDEBUG(("xmgetmd(): got MD at %p\n",md)); } else avail++; } } if (!md) { KDEBUG(("xmgetmd(): MDBLOCK at %p is invalid, no free entries\n",mdb)); return NULL; } /* * remove full MDBLOCK from mdb chain */ if (avail == 0) { KDEBUG(("xmgetmd(): MDBLOCK at %p is now full\n",mdb)); if (unlink_mdblock(mdb) == 0) KDEBUG(("xmgetmd(): removed MDBLOCK at %p from mdb chain\n",mdb)); } return md; }