Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PJE
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <stdbool.h> // #include "map.h" /** * Copyright (c) 2014 rxi * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. */ #ifndef MAP_H #define MAP_H #define MAP_VERSION "0.1.0" struct map_node_t; typedef struct map_node_t map_node_t; typedef struct { map_node_t **buckets; unsigned nbuckets, nnodes; } map_base_t; typedef struct { unsigned bucketidx; map_node_t *node; } map_iter_t; #define map_t(T)\ struct { map_base_t base; T *ref; T tmp; } #define map_init(m)\ memset(m, 0, sizeof(*(m))) #define map_deinit(m)\ map_deinit_(&(m)->base) #define map_get(m, key)\ ( (m)->ref = map_get_(&(m)->base, key) ) #define map_set(m, key, value)\ ( (m)->tmp = (value),\ map_set_(&(m)->base, key, &(m)->tmp, sizeof((m)->tmp)) ) #define map_remove(m, key)\ map_remove_(&(m)->base, key) #define map_iter(m)\ map_iter_() #define map_next(m, iter)\ map_next_(&(m)->base, iter) void map_deinit_(map_base_t *m); void *map_get_(map_base_t *m, const char *key); int map_set_(map_base_t *m, const char *key, void *value, int vsize); void map_remove_(map_base_t *m, const char *key); map_iter_t map_iter_(void); const char *map_next_(map_base_t *m, map_iter_t *iter); typedef map_t(void*) map_void_t; typedef map_t(char*) map_str_t; typedef map_t(int) map_int_t; typedef map_t(char) map_char_t; typedef map_t(float) map_float_t; typedef map_t(double) map_double_t; #endif /** * Copyright (c) 2014 rxi * * This library is free software; you can redistribute it and/or modify it * under the terms of the MIT license. See LICENSE for details. */ #include <stdlib.h> #include <string.h> //#include "map.h" struct map_node_t { unsigned hash; void *value; map_node_t *next; /* char key[]; */ /* char value[]; */ }; static unsigned map_hash(const char *str) { unsigned hash = 5381; while (*str) { hash = ((hash << 5) + hash) ^ *str++; } return hash; } static map_node_t *map_newnode(const char *key, void *value, int vsize) { map_node_t *node; int ksize = strlen(key) + 1; int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*)); node = malloc(sizeof(*node) + voffset + vsize); if (!node) return NULL; memcpy(node + 1, key, ksize); node->hash = map_hash(key); node->value = ((char*) (node + 1)) + voffset; memcpy(node->value, value, vsize); return node; } static int map_bucketidx(map_base_t *m, unsigned hash) { /* If the implementation is changed to allow a non-power-of-2 bucket count, * the line below should be changed to use mod instead of AND */ return hash & (m->nbuckets - 1); } static void map_addnode(map_base_t *m, map_node_t *node) { int n = map_bucketidx(m, node->hash); node->next = m->buckets[n]; m->buckets[n] = node; } static int map_resize(map_base_t *m, int nbuckets) { map_node_t *nodes, *node, *next; map_node_t **buckets; int i; /* Chain all nodes together */ nodes = NULL; i = m->nbuckets; while (i--) { node = (m->buckets)[i]; while (node) { next = node->next; node->next = nodes; nodes = node; node = next; } } /* Reset buckets */ buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets); if (buckets != NULL) { m->buckets = buckets; m->nbuckets = nbuckets; } if (m->buckets) { memset(m->buckets, 0, sizeof(*m->buckets) * m->nbuckets); /* Re-add nodes to buckets */ node = nodes; while (node) { next = node->next; map_addnode(m, node); node = next; } } /* Return error code if realloc() failed */ return (buckets == NULL) ? -1 : 0; } static map_node_t **map_getref(map_base_t *m, const char *key) { unsigned hash = map_hash(key); map_node_t **next; if (m->nbuckets > 0) { next = &m->buckets[map_bucketidx(m, hash)]; while (*next) { if ((*next)->hash == hash && !strcmp((char*) (*next + 1), key)) { return next; } next = &(*next)->next; } } return NULL; } void map_deinit_(map_base_t *m) { map_node_t *next, *node; int i; i = m->nbuckets; while (i--) { node = m->buckets[i]; while (node) { next = node->next; free(node); node = next; } } free(m->buckets); } void *map_get_(map_base_t *m, const char *key) { map_node_t **next = map_getref(m, key); return next ? (*next)->value : NULL; } int map_set_(map_base_t *m, const char *key, void *value, int vsize) { int n, err; map_node_t **next, *node; /* Find & replace existing node */ next = map_getref(m, key); if (next) { memcpy((*next)->value, value, vsize); return 0; } /* Add new node */ node = map_newnode(key, value, vsize); if (node == NULL) goto fail; if (m->nnodes >= m->nbuckets) { n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1; err = map_resize(m, n); if (err) goto fail; } map_addnode(m, node); m->nnodes++; return 0; fail: if (node) free(node); return -1; } void map_remove_(map_base_t *m, const char *key) { map_node_t *node; map_node_t **next = map_getref(m, key); if (next) { node = *next; *next = (*next)->next; free(node); m->nnodes--; } } map_iter_t map_iter_(void) { map_iter_t iter; iter.bucketidx = -1; iter.node = NULL; return iter; } const char *map_next_(map_base_t *m, map_iter_t *iter) { if (iter->node) { iter->node = iter->node->next; if (iter->node == NULL) goto nextBucket; } else { nextBucket: do { if (++iter->bucketidx >= m->nbuckets) { return NULL; } iter->node = m->buckets[iter->bucketidx]; } while (iter->node == NULL); } return (char*) (iter->node + 1); } // Program to print all permutations of a // string in sorted order. /* Following function is needed for library function qsort(). */ int compare(const void* a, const void* b) { return (*(char*)a - *(char*)b); } // A utility function two swap two characters // a and b void swap(char* a, char* b) { char t = *a; *a = *b; *b = t; } // This function finds the index of the // smallest character which is greater // than 'first' and is present in str[l..h] int findCeil(char str[], char first, int l, int h) { // initialize index of ceiling element int ceilIndex = l; // Now iterate through rest of the // elements and find the smallest // character greater than 'first' for (int i = l + 1; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } // Print all permutations of str in sorted order char * sortedPerms(char str[]) { // Get size of string int size = strlen(str); //for use in PJE map char* tempXVal = malloc(sizeof(char) * 10); // Sort the string in increasing order qsort(str, size, sizeof(str[0]), compare); // Print permutations one by one bool isFinished = false; int count = 0; while (!isFinished) { // print this permutation static int x = 1; // Find the rightmost character // which is smaller than its next // character. Let us call it 'first // char' int i; for (i = size - 2; i >= 0; --i) if (str[i] < str[i + 1]) break; // If there is no such character, all // are sorted in decreasing order, // means we just printed the last // permutation and we are done. if (i == -1) { isFinished = true; } else { // Find the ceil of 'first char' // in right of first character. // Ceil of a character is the // smallest character greater // than it int ceilIndex = findCeil(str, str[i], i + 1, size - 1); // Swap first and second characters swap(&str[i], &str[ceilIndex]); // Sort the string on right of 'first char' qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare); } } return tempXVal; } /* Constraints: input is in the form of (\+|-)?[0-9]+ * and without leading zero (0 itself can be as "0" or "+0", but not "-0"); * input pointer is realloc'able and may change; * if input has leading + sign, return may or may not keep it. * The constranits conform to sprintf("%+d") and this function's own output. */ char * incr(char *s){ int i, begin, tail, len; int neg = (*s == '-'); char tgt = neg ? '0' : '9'; /* special case: "-1" */ if (!strcmp(s, "-1")) { s[0] = '0', s[1] = '\0'; return s; } len = strlen(s); begin = (*s == '-' || *s == '+') ? 1 : 0; /* find out how many digits need to be changed */ for (tail = len - 1; tail >= begin && s[tail] == tgt; tail--); if (tail < begin && !neg) { /* special case: all 9s, string will grow */ if (!begin) s = realloc(s, len + 2); s[0] = '1'; for (i = 1; i <= len - begin; i++) s[i] = '0'; s[len + 1] = '\0'; } else if (tail == begin && neg && s[1] == '1') { /* special case: -1000..., so string will shrink */ for (i = 1; i < len - begin; i++) s[i] = '9'; s[len - 1] = '\0'; } else { /* normal case; change tail to all 0 or 9, change prev digit by 1*/ for (i = len - 1; i > tail; i--) s[i] = neg ? '9' : '0'; s[tail] += neg ? -1 : 1; } return s; } int main() { double cov[3][3]; double sum; // Covariance of input data, will be used. Generated using matlab func. // Cannot have negatives, so it will either be turned to 0 or used as abs // // 0.5024 0.0031 0.0107 // 0.0031 0.5284 -0.0207 // 0.0107 -0.0207 0.5022 // //input: https://pastebin.com/TMJU2WDh // lam_11 + (sum ->n of lam1_j) lam_12 lam_13 // for(int i = 0; i < 3; ++i){ for(int j = 0; j < 3; ++j){ scanf("%lf", &cov[i][j]); } } for(int i = 0; i < 3; ++i){ for(int j = 0; j < 3; ++j){ printf("%lf\t", cov[i][j]); } printf("\n"); } for(int i = 0; i < 3; ++i){ for(int j = 0; j < 3; ++j){ if ( i == j ){ sum += fabs(cov[i][j]); } } } printf("Sum: %lf\n", sum); double prob0 = exp(-sum); printf("%lf\n", prob0); double lam_11 = cov[0][0] - (cov[0][1] + cov[0][2]); printf("%lf\n", lam_11); //P(x_1, x_2, x_3, ... x_n) // prob100 = (lam_11 + prob000)/1; //where p(1,0,0) and p(x_1,x_2,x_3) //confused on the "trick" // X_1*P(X_1,X_2,X_3) = lam_11*P(x_1 - 1, x_2, x_3) + lam_12*P(x_1 - 1, x_2 - 1, x_3) + lam_13*P(x_1 -1, x_2, x_3 - 1) printf("\n \n"); // 3 base P(X) //double prob100 = prob000 * lam_11; //double prob010 = prob000 * lam_22; //double prob001 = prob000 * lam_33; //TODO dynamically find diag lambdas (done) // implement trick dynamically?? // make the possibility of parallelization possible (loops, etc) // n is the max position of where lamb_nn int n = 3; double *lam = malloc(sizeof(double)*n); double lamSum; int count = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ lamSum += fabs(cov[i][j]); printf("LS: %lf\n", lamSum); } //calculates lam_nn lam[i] = cov[i][count] - (lamSum - cov[i][count]); lamSum = 0; count++; } for(int i = 0; i < n; i++){ printf("\nLambda %d%d = %lf\n",i+1,i+1,lam[i]); } double prevProb; //returns max value char maxVal( char x[] ){ char maxVal = x[0]; int n = strlen(x); for(int i = 0; i < n; i++){ if( x[i] > maxVal ){ maxVal = x[i]; } } return maxVal; } int maxIntVal( char x[] ){ int maxIntVal = x[0]; int n = strlen(x); for(int i = 0; i < n; i++){ if( x[i] > maxIntVal ){ maxIntVal = x[i]; } } return maxVal; } int mIndex(char *a){ if(n <= 0) return -1; int i, max_i = 0; char max = a[0]; int n = strlen(a); for(i = 1; i < n; ++i){ if(a[i] > max){ max = a[i]; max_i = i; } } return max_i; } //returns # of zeros int numZeros(char* arr){ int n = strlen(arr); int zerCount = 0; for( int i = 0; i < n; i++){ if( arr[i] == '0' ){ zerCount++; } } return zerCount; } //returns positions of Non Zeros // creates an array same size as input, may be needed int* posNZ(char *arr){ int n = strlen(arr); int j = 0; int* pos = (int*) malloc(n * sizeof(int)); for( int i = 0; i < n; i++){ if( arr[i] != '0' ){ pos[j] = i; j++; } } return pos; } //returns # of nonzeros int numNon(char* arr){ int n = strlen(arr); int nonZero = 0; for( int i = 0; i < n; i++){ if( arr[i] != '0' && arr[i] <= '9' ){ nonZero++; } } return nonZero; } //returns position of int* compare(char* arr, char* arr2){ int n = strlen(arr); int* pos = (int*) malloc(n * sizeof(int)); for(int i = 0; i < n; i++){ if( arr[i] != arr2[i] ){ pos[i] = i; } } return pos; } //takes in array to be subtracted and at what position, //returns copy of string passed that's decrecemented char* sub(char* arr, int pos){ int len = strlen(arr); char* temp = (char*) malloc(len * sizeof(char)); strcpy(temp, arr); int n = strlen(temp); for(int i = 0; i < n; i++){ if( i == pos ){ --temp[i]; } } return temp; } void fill(char ***adr,int *pos,char *pref) { int i,z=1; //loop on the chars, and check if should use them for (i=0;i<256;i++) if (pos[i]) { int l=strlen(pref); //add the char pref[l]=i; pos[i]--; //call the recursion fill(adr,pos,pref); //delete the char pref[l]=0; pos[i]++; z=0; } if (z) strcpy(*(*adr)++,pref); } //input: blank array to store perms in, and array to perm void perm(char **arr,const char *str) { int p[256]={0}; int l = strlen(str); char temp[l+1]; for (;l>=0;l--){ temp[l]=0; } while (*str){ p[*str++]++; } fill(&arr,p,temp); } char fucking[] = "011"; char *cunt[10]; for(int i = 0; i < 10; i++) cunt[i] = malloc(sizeof(fucking)); perm(cunt,fucking); for( int i = 0 ; i < 10; i++) printf("%d %s\n",i, cunt[i]); //for (x1, 0,0) cases swap poisitions for rest //if x1 is max value && rest of x[] == 0 then prob = lam_11 * p(x+1,0,0) int x[] = {0,0,0}; //printf("Case 000: %f", func(x)); //prob(x[]){ // lam_11*prob(x[i] + 1, x[i], x[i]) + lam_12*prob() //} //so for calculations, you can use a map or a DLL to find the .prev elements and put it into the .next //probability char xVal[] = "000"; incr(xVal); for(int i =0; i < 3; i++){ printf("aaaaaaaa : %c\n", xVal[i]); } //incr(p); //leave as string to use string functions, covert to int to do calculation, reconvert back to store in table? char p[] = "001"; sortedPerms(p); map_double_t m; map_init(&m); char test[] = "000"; int lenth = strlen(test); char a = '0'; a++; printf("%c\n", a); printf("Num non zeros in p %d\n", numNon(p)); char* tempX = "0"; double prob(char* xVals){ //sets all values of starting tempX to 0 size_t n = strlen(xVals); if (strlen(tempX) < n){ tempX = (char*) calloc(n, sizeof(char*)); int size = strlen(tempX); // printf("Len of tempX: %d\n", size); for(int i = 0; i < n; i++){ tempX[i] = '0'; // printf("temp x : %c\n", tempX[i]); // printf("Len of temp: %d\n", strlen(tempX)); } } if( maxVal(tempX) == '0' ){ map_set(&m, tempX, exp(-sum)); tempX[n-1]++; for(int i =0; i < 3; i++){ printf("%c", tempX[i]); } } //one is nonzero and the rest are zeros if(( maxVal(tempX) >= 1) && (((maxVal(tempX) + numZeros(tempX) - '0' )) == n )){ char *storePerms[n]; for(int i = 0; i < n; i++) storePerms[i] = malloc(sizeof(char)); perm(storePerms,tempX); for(int i = 0; i < n; i++){ char *subPrev[n]; subPrev[i] = malloc(sizeof(char)); int j = mIndex(storePerms[i]); subPrev[i] = (sub(storePerms[i], mIndex(storePerms[i]))); double mtemp = *map_get(&m, subPrev[i]); double ptemp = ( (lam[j] * mtemp) / ((double)maxVal(storePerms[i]) - '0') ); map_set(&m, storePerms[i], ptemp); } } char tempY[] = "001"; //more than one is nonzero and at least one is zero if( (numNon(tempX)>= 1) && ((numZeros(tempX)) >= 1) && ((numZeros(tempX)) + (numNon(tempX)) == n)){ printf("\nI'm in bitches\n"); //if max is at the end, increment the middle if (mIndex(tempX) == n-1){ int middle = (strlen(tempX)/2); tempX[middle]++; } char *storePerms[n]; for(int i = 0; i < n; i++) storePerms[i] = malloc(sizeof(char)); perm(storePerms,tempX); //now that the posNZ position of nonzeros exists, possible to incr/ find lam/cov values //based on that for(int i = 0; i < n; i++){ //reinit array char *subPrev[n]; int j = 0; subPrev[i] = malloc(sizeof(char)); int lenCase2 = (strlen(storePerms[i]) - numZeros(storePerms[i])); char *toZero[n]; toZero[i] = malloc(sizeof(char)); //toZero[i] = malloc(sizeof(char)); subPrev[i] = (sub(storePerms[i],posNZ(storePerms[i])[j])); //returns index of difference int k = compare(storePerms[i], subPrev[i]); double mtemp = *map_get(&m, subPrev[i]); //use in another for loop to determine for if there is more than one difference ._. int *tempNZ = posNZ(subPrev[i]); //it is not doing the subtraction? fix and finish finding the probability and storing them, after that is just the last case and figuring out how to go from P(2,2,2) to an output!!!!!! //:) for(int l = 0; l < strlen(toZero); l++){ toZero[l] = (sub(subPrev[i], tempNZ[l])); printf("FAK: %s", toZero[l]); } //double ptemp = ( (lam[k] * mtemp) + (cov[][] * )) if( j < lenCase2){ j++; } printf("\nHDHdD: %s\n", subPrev[i]); // } for(int i = 0; i < n; i++){ printf("Case 2 perms: %s\n", storePerms[i]); } } const char *key; map_iter_t iter = map_iter(&m); while ((key = map_next(&m, &iter))) { printf("\n %s -> %lf\n", key, *map_get(&m, key)); } } char wow[] = "000"; prob(wow); char pizza[] = "110"; char* cheese = posNZ(pizza); int dd = (strlen(pizza) - numZeros(pizza)); for(int i = 0; i < dd; i++){ printf("NZ: %d\n", ((posNZ(pizza)[i]) )); } int boof = strlen(pizza); // for(int i = 0; i < boof; i++){ // for(int j = 0; j < dd; j++){ // printf("Ah geeez: %s", (sub(pizza, posNZ(pizza)[j]))); // } //} return 0; }
gcc
0.5024 0.0031 0.0107 0.0031 0.5284 -0.0207 0.0107 -0.0207 0.5022
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 0.34 sec, absolute running time: 0.08 sec, cpu time: 0.01 sec, memory peak: 3 Mb, absolute service time: 0,43 sec
edit mode
|
history
|
discussion