Exemplo n.º 1
0
int
cmd_sample (struct lexer *lexer, struct dataset *ds)
{
    struct sample_trns *trns;

    int type;
    int a, b;
    unsigned frac;

    if (!lex_force_num (lexer))
        return CMD_FAILURE;
    if (!lex_is_integer (lexer))
    {
        unsigned long min = gsl_rng_min (get_rng ());
        unsigned long max = gsl_rng_max (get_rng ());

        type = TYPE_FRACTION;
        if (lex_tokval (lexer) <= 0 || lex_tokval (lexer) >= 1)
        {
            msg (SE, _("The sampling factor must be between 0 and 1 "
                       "exclusive."));
            return CMD_FAILURE;
        }

        frac = lex_tokval (lexer) * (max - min) + min;
        a = b = 0;
    }
    else
    {
        type = TYPE_A_FROM_B;
        a = lex_integer (lexer);
        lex_get (lexer);
        if (!lex_force_match_id (lexer, "FROM"))
            return CMD_FAILURE;
        if (!lex_force_int (lexer))
            return CMD_FAILURE;
        b = lex_integer (lexer);
        if (a >= b)
        {
            msg (SE, _("Cannot sample %d observations from a population of "
                       "%d."),
                 a, b);
            return CMD_FAILURE;
        }

        frac = 0;
    }
    lex_get (lexer);

    trns = xmalloc (sizeof *trns);
    trns->type = type;
    trns->n = a;
    trns->N = b;
    trns->m = trns->t = 0;
    trns->frac = frac;
    add_transformation (ds, sample_trns_proc, sample_trns_free, trns);

    return CMD_SUCCESS;
}
Exemplo n.º 2
0
/*
 * Generates a random directed weighted graph with N nodes and K edges.  Weights
 * are chosen uniformly between wmin and wmax.  No edges are placed on the main
 * diagonal.
 */
MATRIX_T* BCT_NAMESPACE::makerandCIJ_wd(int N, int K, FP_T wmin, FP_T wmax) {
	gsl_rng* rng = get_rng();
	VECTOR_T* w = VECTOR_ID(alloc)(K);
	for (int i = 0; i < K; i++) {
		VECTOR_ID(set)(w, i, gsl_rng_uniform(rng) * (wmax - wmin) + wmin);
	}
	
	// ind = ~eye(N);
	MATRIX_T* eye_N = eye(N);
	MATRIX_T* ind = logical_not(eye_N);
	MATRIX_ID(free)(eye_N);
	
	// i = find(ind);
	VECTOR_T* i = find(ind);
	MATRIX_ID(free)(ind);
	
	// rp = randperm(length(i));
	gsl_permutation* rp = randperm(length(i));
	
	// irp = i(rp);
	VECTOR_T* irp = permute(rp, i);
	gsl_permutation_free(rp);
	VECTOR_ID(free)(i);
	
	// CIJ = zeros(N);
	MATRIX_T* CIJ = zeros(N);
	
	// CIJ(irp(1:K)) = w;
	VECTOR_ID(view) irp_subv = VECTOR_ID(subvector)(irp, 0, K);
	ordinal_index_assign(CIJ, &irp_subv.vector, w);
	VECTOR_ID(free)(w);
	VECTOR_ID(free)(irp);
	
	return CIJ;
}
Exemplo n.º 3
0
/*
 * Generates a random directed weighted graph with the same number of nodes,
 * number of edges, and weight distribution as the given graph.  No edges are
 * placed on the main diagonal.  The given matrix should therefore not contain
 * nonzero entries on the main diagonal.
 */
MATRIX_T* BCT_NAMESPACE::makerandCIJ_wd_wp(const MATRIX_T* m) {
	if (safe_mode) check_status(m, SQUARE | NO_LOOPS, "makerandCIJ_wd_wp");
	int N = m->size1;
	int K = N * (N - 1);
	FP_T* w = new FP_T[K];
	for (int i = 0, k = 0; i < (int)m->size1; i++) {
		for (int j = 0; j < (int)m->size2; j++) {
			if (i != j) {
				w[k++] = MATRIX_ID(get)(m, i, j);
			}
		}
	}
	gsl_rng* rng = get_rng();
	gsl_ran_shuffle(rng, w, K, sizeof(FP_T));
	MATRIX_T* rand_m = MATRIX_ID(alloc)(m->size1, m->size2);
	for (int i = 0, k = 0; i < (int)m->size1; i++) {
		for (int j = 0; j < (int)m->size2; j++) {
			if (i == j) {
				MATRIX_ID(set)(rand_m, i, j, 0.0);
			} else {
				MATRIX_ID(set)(rand_m, i, j, w[k++]);
			}
		}
	}
	delete[] w;
	return rand_m;
}
Exemplo n.º 4
0
bool tr_rand_buffer(void* buffer, size_t length)
{
    bool ret;
    tr_lock* rng_lock = get_rng_lock();

    TR_ASSERT(buffer != NULL);

    tr_lockLock(rng_lock);
    ret = check_result(API(RNG_GenerateBlock)(get_rng(), buffer, length));
    tr_lockUnlock(rng_lock);

    return ret;
}
Exemplo n.º 5
0
bool
tr_dh_make_key (tr_dh_ctx_t   raw_handle,
                size_t        private_key_length UNUSED,
                uint8_t     * public_key,
                size_t      * public_key_length)
{
  struct tr_dh_ctx * handle = raw_handle;
  word32 my_private_key_length, my_public_key_length;
  tr_lock * rng_lock = get_rng_lock ();

  assert (handle != NULL);
  assert (public_key != NULL);

  if (handle->private_key == NULL)
    handle->private_key = tr_malloc (handle->key_length);

  tr_lockLock (rng_lock);

  if (!check_result (DhGenerateKeyPair (&handle->dh, get_rng (),
                                        handle->private_key, &my_private_key_length,
                                        public_key, &my_public_key_length)))
    {
      tr_lockUnlock (rng_lock);
      return false;
    }

  tr_lockUnlock (rng_lock);

  tr_dh_align_key (public_key, my_public_key_length, handle->key_length);

  handle->private_key_length = my_private_key_length;

  if (public_key_length != NULL)
    *public_key_length = handle->key_length;

  return true;
}
Exemplo n.º 6
0
  void Botan_server::load_credentials(
    const std::string& server_name,
    fs::Dirent& file_ca_key,
    fs::Dirent& file_ca_cert,
    fs::Dirent& file_server_key)
  {
    // load CA certificate
    assert(file_ca_cert.is_valid());
    auto ca_cert = file_ca_cert.read();
    std::vector<uint8_t> vca_cert(ca_cert.begin(), ca_cert.end());
    // load CA private key
    auto ca_key = read_pkey(file_ca_key);
    // load server private key
    auto srv_key = read_pkey(file_server_key);

    auto* credman = net::Credman::create(
            server_name,
            get_rng(),
            std::move(ca_key),
            Botan::X509_Certificate(vca_cert),
            std::move(srv_key));

    this->credman.reset(credman);
  }
Exemplo n.º 7
0
inline std::unique_ptr<Botan::Private_Key> read_pkey(fs::Dirent& key_file)
{
  assert(key_file.is_file());
  Botan::DataSource_Memory data{key_file.read()};
  return std::unique_ptr<Botan::Private_Key>(Botan::PKCS8::load_key(data, get_rng()));
}
Exemplo n.º 8
0
/*
 * Generates a random directed binary graph with the given in-degree and out-
 * degree sequences.  Returns NULL if the algorithm failed to generate a graph
 * satisfying the given degree sequences.
 */
MATRIX_T* BCT_NAMESPACE::makerandCIJdegreesfixed(const VECTOR_T* in, const VECTOR_T* out) {
	gsl_rng* rng = get_rng();
	
	// n = length(in);
	int n = length(in);
	
	// k = sum(in);
	int k = sum(in);
	
	// inInv = zeros(k,1);
	VECTOR_T* inInv = zeros_vector(k);
	
	// outInv = inInv;
	VECTOR_T* outInv = copy(inInv);
	
	// iIn = 1; iOut = 1;
	int iIn = 0;
	int iOut = 0;
	
	// for i = 1:n
	for (int i = 0; i < n; i++) {
		
		// inInv(iIn:iIn+in(i) - 1) = i;
		VECTOR_T* inInv_ind = sequence(iIn, iIn + (int)VECTOR_ID(get)(in, i) - 1);
		if (inInv_ind != NULL) {
			ordinal_index_assign(inInv, inInv_ind, (FP_T)i);
			VECTOR_ID(free)(inInv_ind);
		}
		
		// outInv(iOut:iOut+out(i) - 1) = i;
		VECTOR_T* outInv_ind = sequence(iOut, iOut + (int)VECTOR_ID(get)(out, i) - 1);
		if (outInv_ind != NULL) {
			ordinal_index_assign(outInv, outInv_ind, (FP_T)i);
			VECTOR_ID(free)(outInv_ind);
		}
		
		// iIn = iIn+in(i);
		iIn += (int)VECTOR_ID(get)(in, i);
		
		// iOut = iOut+out(i);
		iOut += (int)VECTOR_ID(get)(out, i);
	}
	
	// cij = eye(n);
	MATRIX_T* cij = eye(n);
	
	// edges = [outInv(1:k)'; inInv(randperm(k))'];
	VECTOR_T* outInv_ind = sequence(0, k - 1);
	VECTOR_T* edges_row_0 = ordinal_index(outInv, outInv_ind);
	VECTOR_ID(free)(outInv);
	VECTOR_ID(free)(outInv_ind);
	gsl_permutation* inInv_ind = randperm(k);
	VECTOR_T* edges_row_1 = permute(inInv_ind, inInv);
	gsl_permutation_free(inInv_ind);
	VECTOR_ID(free)(inInv);
	MATRIX_T* edges = concatenate_columns(edges_row_0, edges_row_1);
	VECTOR_ID(free)(edges_row_0);
	VECTOR_ID(free)(edges_row_1);
	
	bool flag = true;
	
	// for i = 1:k
	for (int i = 0; i < k && flag; i++) {
		
		// if cij(edges(1,i),edges(2,i)),
		if (fp_nonzero(MATRIX_ID(get)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, i)))) {
			
			// warningCounter = 1;
			int warningCounter = 1;
			
			// while (1)
			while (true) {
				
				// switchTo = ceil(k*rand);
				int switchTo = (int)std::ceil((k - 1) * gsl_rng_uniform(rng));
				
				// if ~(cij(edges(1,i),edges(2,switchTo)) || cij(edges(1,switchTo),edges(2,i))),
				if (!(fp_nonzero(MATRIX_ID(get)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, switchTo))) ||
					  fp_nonzero(MATRIX_ID(get)(cij, (int)MATRIX_ID(get)(edges, 0, switchTo), (int)MATRIX_ID(get)(edges, 1, i))))) {
					
					// cij(edges(1,i),edges(2,switchTo)) = 1;
					MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, switchTo), 1.0);
					
					// if switchTo < i,
					if (switchTo < i) {
						
						// cij(edges(1,switchTo),edges(2,switchTo)) = 0;
						MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, switchTo), (int)MATRIX_ID(get)(edges, 1, switchTo), 0.0);
						
						// cij(edges(1,switchTo),edges(2,i)) = 1;
						MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, switchTo), (int)MATRIX_ID(get)(edges, 1, i), 1.0);
					}
					
					// temp = edges(2,i);
					FP_T temp = MATRIX_ID(get)(edges, 1, i);
					
					// edges(2,i) = edges(2,switchTo);
					MATRIX_ID(set)(edges, 1, i, MATRIX_ID(get)(edges, 1, switchTo));
					
					// edges(2,switchTo) = temp;
					MATRIX_ID(set)(edges, 1, switchTo, temp);
					
					// break
					break;
				}
				
				// warningCounter = warningCounter+1;
				warningCounter++;
				
				// if warningCounter == 2*k^2
				if (warningCounter == 2 * k * k) {
					
					// flag = 0;
					flag = false;
					
					// return;
					break;
				}
			}
		} else {
			
			// cij(edges(1,i),edges(2,i)) = 1;
			MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, i), 1.0);
		}
	}

	MATRIX_ID(free)(edges);
	
	// flag = 1;
	if (!flag) {
		MATRIX_ID(free)(cij);
		return NULL;
	}
	
	// cij = cij - eye(n);
	MATRIX_T* eye_n = eye(n);
	MATRIX_ID(sub)(cij, eye_n);
	MATRIX_ID(free)(eye_n);
	
	return cij;
}
Exemplo n.º 9
0
/*
 * Returns a latticized graph with equivalent degree sequence to the original
 * weighted undirected graph.  On average, each edge is rewired ITER times.
 * Strength distributions are not preserved for weighted graphs.
 */
MATRIX_T* BCT_NAMESPACE::latmio_und(const MATRIX_T* R, int ITER) {
    if (safe_mode) check_status(R, SQUARE | UNDIRECTED, "latmio_und");

    gsl_rng* rng = get_rng();

    // n=length(R);
    int n = length(R);

    // D=zeros(n);
    MATRIX_T* D = zeros(n);

    // u=[0 min([mod(1:n-1,n);mod(n-1:-1:1,n)])];
    VECTOR_T* seq1 = sequence(1, n - 1);
    VECTOR_T* seq2 = sequence(n - 1, -1, 1);
    MATRIX_T* seq1_seq2 = concatenate_columns(seq1, seq2);
    VECTOR_ID(free)(seq1);
    VECTOR_ID(free)(seq2);
    VECTOR_T* min_seq1_seq2 = min(seq1_seq2);
    MATRIX_ID(free)(seq1_seq2);
    VECTOR_T* u = concatenate(0.0, min_seq1_seq2);
    VECTOR_ID(free)(min_seq1_seq2);

    // for v=1:ceil(n/2)
    for (int v = 1; v <= (int)std::ceil((FP_T)n / 2.0); v++) {

        // D(n-v+1,:)=u([v+1:n 1:v]);
        VECTOR_T* u_indices1 = sequence(v, n - 1);
        VECTOR_T* u_indices2 = sequence(0, v - 1);
        VECTOR_T* u_indices = concatenate(u_indices1, u_indices2);
        VECTOR_ID(free)(u_indices1);
        VECTOR_ID(free)(u_indices2);
        VECTOR_T* u_idx = ordinal_index(u, u_indices);
        VECTOR_ID(free)(u_indices);
        MATRIX_ID(set_row)(D, n - v, u_idx);
        VECTOR_ID(free)(u_idx);

        // D(v,:)=D(n-v+1,n:-1:1);
        VECTOR_T* D_rows = VECTOR_ID(alloc)(1);
        VECTOR_ID(set)(D_rows, 0, (FP_T)(n - v));
        VECTOR_T* D_cols = sequence(n - 1, -1, 0);
        MATRIX_T* D_idx = ordinal_index(D, D_rows, D_cols);
        VECTOR_ID(free)(D_rows);
        VECTOR_ID(free)(D_cols);
        VECTOR_T* D_idx_v = to_vector(D_idx);
        MATRIX_ID(free)(D_idx);
        MATRIX_ID(set_row)(D, v - 1, D_idx_v);
        VECTOR_ID(free)(D_idx_v);
    }

    // [i j]=find(tril(R));
    MATRIX_T* tril_R = tril(R);
    MATRIX_T* find_tril_R = find_ij(tril_R);
    MATRIX_ID(free)(tril_R);
    VECTOR_ID(view) i = MATRIX_ID(column)(find_tril_R, 0);
    VECTOR_ID(view) j = MATRIX_ID(column)(find_tril_R, 1);

    // K=length(i);
    int K = length(&i.vector);

    // ITER=K*ITER;
    ITER = K * ITER;

    MATRIX_T* _R = copy(R);

    // for iter=1:ITER
    for (int iter = 1; iter <= ITER; iter++) {

        // while 1
        while (true) {

            int e1, e2;
            int a, b, c, d;

            // while 1
            while (true) {

                // e1=ceil(K*rand);
                e1 = gsl_rng_uniform_int(rng, K);

                // e2=ceil(K*rand);
                e2 = gsl_rng_uniform_int(rng, K);

                // while (e2==e1),
                while (e2 == e1) {

                    // e2=ceil(K*rand);
                    e2 = gsl_rng_uniform_int(rng, K);
                }

                // a=i(e1); b=j(e1);
                a = (int)VECTOR_ID(get)(&i.vector, e1);
                b = (int)VECTOR_ID(get)(&j.vector, e1);

                // c=i(e2); d=j(e2);
                c = (int)VECTOR_ID(get)(&i.vector, e2);
                d = (int)VECTOR_ID(get)(&j.vector, e2);

                // if all(a~=[c d]) && all(b~=[c d]);
                if (a != c && a != d && b != c && b != d) {

                    // break
                    break;
                }
            }

            // if rand>0.5
            if (gsl_rng_uniform(rng) > 0.5) {

                // i(e2)=d; j(e2)=c;
                VECTOR_ID(set)(&i.vector, e2, (FP_T)d);
                VECTOR_ID(set)(&j.vector, e2, (FP_T)c);

                // c=i(e2); d=j(e2);
                c = (int)VECTOR_ID(get)(&i.vector, e2);
                d = (int)VECTOR_ID(get)(&j.vector, e2);
            }

            // if ~(R(a,d) || R(c,b))
            if (fp_zero(MATRIX_ID(get)(_R, a, d)) && fp_zero(MATRIX_ID(get)(_R, c, b))) {

                // if (D(a,b)+D(c,d))>=(D(a,d)+D(c,b))
                if (fp_greater_or_equal(MATRIX_ID(get)(D, a, b) + MATRIX_ID(get)(D, c, d),
                                        MATRIX_ID(get)(D, a, d) + MATRIX_ID(get)(D, c, b))) {

                    // R(a,d)=R(a,b); R(a,b)=0;
                    MATRIX_ID(set)(_R, a, d, MATRIX_ID(get)(_R, a, b));
                    MATRIX_ID(set)(_R, a, b, 0.0);

                    // R(d,a)=R(b,a); R(b,a)=0;
                    MATRIX_ID(set)(_R, d, a, MATRIX_ID(get)(_R, b, a));
                    MATRIX_ID(set)(_R, b, a, 0.0);

                    // R(c,b)=R(c,d); R(c,d)=0;
                    MATRIX_ID(set)(_R, c, b, MATRIX_ID(get)(_R, c, d));
                    MATRIX_ID(set)(_R, c, d, 0.0);

                    // R(b,c)=R(d,c); R(d,c)=0;
                    MATRIX_ID(set)(_R, b, c, MATRIX_ID(get)(_R, d, c));
                    MATRIX_ID(set)(_R, d, c, 0.0);

                    // j(e1) = d;
                    VECTOR_ID(set)(&j.vector, e1, (FP_T)d);

                    // j(e2) = b;
                    VECTOR_ID(set)(&j.vector, e2, (FP_T)b);

                    // break;
                    break;
                }
            }
        }
    }

    MATRIX_ID(free)(D);
    MATRIX_ID(free)(find_tril_R);
    return _R;
}