AnyType TPTScriptInterface::tptS_delete(std::deque<std::string> * words) { //Arguments from stack AnyType partRef = eval(words); Simulation * sim = m->GetSimulation(); if(partRef.GetType() == TypePoint) { ui::Point deletePoint = ((PointType)partRef).Value(); if(deletePoint.X<0 || deletePoint.Y<0 || deletePoint.Y >= YRES || deletePoint.X >= XRES) throw GeneralException("Invalid position"); sim->delete_part(deletePoint.X, deletePoint.Y); } else if(partRef.GetType() == TypeNumber) { int partIndex = ((NumberType)partRef).Value(); if(partIndex < 0 || partIndex >= NPART) throw GeneralException("Invalid particle index"); sim->kill_part(partIndex); } else throw GeneralException("Invalid particle reference"); return NumberType(0); }
AnyType chi2_gof_test_transition::run(AnyType &args) { // §4.15.4 ("Aggregate functions") of ISO/IEC 9075-2:2003, "SQL/Foundation" // demands that rows containing NULLs are ignored // We currently rely on the backend filtering out rows with NULLs, i.e., to // perform an action equivalent to: // for (uint16_t i = 0; i < args.numFields(); ++i) // if (args[i].isNull) // return state; Chi2TestTransitionState<MutableArrayHandle<double> > state = args[0]; double observed = static_cast<double>(args[1].getAs<int64_t>()); double expected = args.numFields() <= 2 ? 1 : args[2].getAs<double>(); int64_t df = args.numFields() <= 3 ? 0 : args[3].getAs<int64_t>(); if (observed < 0) throw std::invalid_argument("Number of observations must be " "nonnegative."); else if (df < 0) throw std::invalid_argument("Degree of freedom must be positive (or 0 " "to use the default of <number of rows> - 1)."); else if (state.df != df) { if (state.numRows > 0) throw std::invalid_argument("Degree of freedom must be constant."); state.df = df; } updateSumSquaredDeviations(state.numRows.ref(), state.sum_expect.ref(), state.sum_obs_square_over_expect.ref(), state.sum_obs.ref(), state.sumSquaredDeviations.ref(), 1, expected, observed * observed / expected, observed, 0); return state; }
AnyType TPTScriptInterface::tptS_create(std::deque<std::string> * words) { //Arguments from stack AnyType createType = eval(words); PointType position = eval(words); Simulation * sim = m->GetSimulation(); int type; if(createType.GetType() == TypeNumber) type = ((NumberType)createType).Value(); else if(createType.GetType() == TypeString) type = GetParticleType(((StringType)createType).Value()); else throw GeneralException("Invalid type"); if(type == -1) throw GeneralException("Invalid particle type"); ui::Point tempPoint = position.Value(); if(tempPoint.X<0 || tempPoint.Y<0 || tempPoint.Y >= YRES || tempPoint.X >= XRES) throw GeneralException("Invalid position"); int returnValue = sim->create_part(-1, tempPoint.X, tempPoint.Y, type); return NumberType(returnValue); }
int DataAccess::setMappings(void *data, int numOfCols, char **row, char **colName){ AnyType *temp; temp=new AnyType(numOfCols); temp->setItem(row[0]? (string)row[0] : "NULL", "input_str"); temp->setItem(row[1]? (string)row[1] : "NULL", "latex"); temp->setItem(row[2]? (string)row[2] : "NULL", "mathml"); temp->setItem(row[3]? (string)row[3] : "NULL", "exp_type"); temp->setItem((bool)(strcmp((char*)row[4],"1")==0? true : false), "brac_essential"); temp->setItem((int)row[5], "no_of_para"); temp->setItem((long long)row[6], "hits"); temp->setItem(row[7]? (string)row[7] : "NULL", "para"); temp->setItem(row[8]? (string)row[8] : "NULL", "input_format"); DataAccess::tokenMappings->push_back(*temp); //WriteToFile(numOfCols, row, colName); return(0); }
/** * @brief Return the Binomial family object: family, linkfun */ AnyType binomial::run(AnyType &args) { std::string linkfunc; // 1. get the linkfun argument: default value -- "identity" if (args.isNull()) linkfunc = "logit"; else linkfunc = args[0].getAs<char*>(); std::transform(linkfunc.begin(), linkfunc.end(), linkfunc.begin(), ::tolower); // 3. return family information std::string family = "binomial"; AnyType tuple; tuple << family << linkfunc; return tuple; }
System::Object^ toObject(const AnyType &source) { if (source.type() == typeid(AnyBoolean)) return boost::any_cast<AnyBoolean>(source); else if (source.type() == typeid(AnyInt64)) return boost::any_cast<AnyInt64>(source); else if (source.type() == typeid(AnyInt32)) return boost::any_cast<AnyInt32>(source); else if (source.type() == typeid(AnyInt16)) return boost::any_cast<AnyInt16>(source); else if (source.type() == typeid(AnyInt8)) return boost::any_cast<AnyInt8>(source); else if (source.type() == typeid(AnyUInt64)) return boost::any_cast<AnyUInt64>(source); else if (source.type() == typeid(AnyUInt32)) return boost::any_cast<AnyUInt32>(source); else if (source.type() == typeid(AnyUInt16)) return boost::any_cast<AnyUInt16>(source); else if (source.type() == typeid(AnyUInt8)) return boost::any_cast<AnyUInt8>(source); else if (source.type() == typeid(AnyFloat128)) return boost::any_cast<AnyFloat128>(source); else if (source.type() == typeid(AnyFloat64)) return boost::any_cast<AnyFloat64>(source); else if (source.type() == typeid(AnyFloat32)) return boost::any_cast<AnyFloat32>(source); else if (source.type() == typeid(AnyDateTime)) return anyDateTimeToDateTime(boost::any_cast<AnyDateTime>(source)); else if (source.type() == typeid(AnyAsciiChar)) return boost::any_cast<AnyAsciiChar>(source); else if (source.type() == typeid(AnyUnicodeChar)) return boost::any_cast<AnyUnicodeChar>(source); else if (source.type() == typeid(AnyAsciiString)) return anyAsciiStringToSystemString(boost::any_cast<AnyAsciiString>(source)); else if (source.type() == typeid(AnyUnicodeString)) return anyUnicodeStringToSystemString(boost::any_cast<AnyUnicodeString>(source)); else if (source.type() == typeid(AnyByteArray)) return anyByteArrayToByteArray(boost::any_cast<AnyByteArray>(source)); else if (source.type() == typeid(AnyAsciiStringArray)) return anyAsciiStringArrayToStringArray(boost::any_cast<AnyAsciiStringArray>(source)); else if (source.type() == typeid(AnyUnicodeStringArray)) return anyUnicodeStringArrayToStringArray(boost::any_cast<AnyUnicodeStringArray>(source)); return nullptr; }
/** * @brief This function learns the topics of words in a document and is the * main step of a Gibbs sampling iteration. The word topic counts and * corpus topic counts are passed to this function in the first call and * then transfered to the rest calls through args.mSysInfo->user_fctx for * efficiency. * @param args[0] The unique words in the documents * @param args[1] The counts of each unique words * @param args[2] The topic counts and topic assignments in the document * @param args[3] The model (word topic counts and corpus topic * counts) * @param args[4] The Dirichlet parameter for per-document topic * multinomial, i.e. alpha * @param args[5] The Dirichlet parameter for per-topic word * multinomial, i.e. beta * @param args[6] The size of vocabulary * @param args[7] The number of topics * @param args[8] The number of iterations (=1:training, >1:prediction) * @return The updated topic counts and topic assignments for * the document **/ AnyType lda_gibbs_sample::run(AnyType & args) { ArrayHandle<int32_t> words = args[0].getAs<ArrayHandle<int32_t> >(); ArrayHandle<int32_t> counts = args[1].getAs<ArrayHandle<int32_t> >(); MutableArrayHandle<int32_t> doc_topic = args[2].getAs<MutableArrayHandle<int32_t> >(); double alpha = args[4].getAs<double>(); double beta = args[5].getAs<double>(); int32_t voc_size = args[6].getAs<int32_t>(); int32_t topic_num = args[7].getAs<int32_t>(); int32_t iter_num = args[8].getAs<int32_t>(); size_t model64_size = static_cast<size_t>(voc_size * (topic_num + 1) + 1) * sizeof(int32_t) / sizeof(int64_t); if(alpha <= 0) throw std::invalid_argument("invalid argument - alpha"); if(beta <= 0) throw std::invalid_argument("invalid argument - beta"); if(voc_size <= 0) throw std::invalid_argument( "invalid argument - voc_size"); if(topic_num <= 0) throw std::invalid_argument( "invalid argument - topic_num"); if(iter_num <= 0) throw std::invalid_argument( "invalid argument - iter_num"); if(words.size() != counts.size()) throw std::invalid_argument( "dimensions mismatch: words.size() != counts.size()"); if(__min(words) < 0 || __max(words) >= voc_size) throw std::invalid_argument( "invalid values in words"); if(__min(counts) <= 0) throw std::invalid_argument( "invalid values in counts"); int32_t word_count = __sum(counts); if(doc_topic.size() != (size_t)(word_count + topic_num)) throw std::invalid_argument( "invalid dimension - doc_topic.size() != word_count + topic_num"); if(__min(doc_topic, 0, topic_num) < 0) throw std::invalid_argument("invalid values in topic_count"); if( __min(doc_topic, topic_num, word_count) < 0 || __max(doc_topic, topic_num, word_count) >= topic_num) throw std::invalid_argument( "invalid values in topic_assignment"); if (!args.getUserFuncContext()) { ArrayHandle<int64_t> model64 = args[3].getAs<ArrayHandle<int64_t> >(); if (model64.size() != model64_size) { std::stringstream ss; ss << "invalid dimension: model64.size() = " << model64.size(); throw std::invalid_argument(ss.str()); } if (__min(model64) < 0) { throw std::invalid_argument("invalid topic counts in model"); } int32_t *context = static_cast<int32_t *>( MemoryContextAllocZero( args.getCacheMemoryContext(), model64.size() * sizeof(int64_t) + topic_num * sizeof(int64_t))); memcpy(context, model64.ptr(), model64.size() * sizeof(int64_t)); int32_t *model = context; int64_t *running_topic_counts = reinterpret_cast<int64_t *>( context + model64_size * sizeof(int64_t) / sizeof(int32_t)); for (int i = 0; i < voc_size; i ++) { for (int j = 0; j < topic_num; j ++) { running_topic_counts[j] += model[i * (topic_num + 1) + j]; } } args.setUserFuncContext(context); } int32_t *context = static_cast<int32_t *>(args.getUserFuncContext()); if (context == NULL) { throw std::runtime_error("args.mSysInfo->user_fctx is null"); } int32_t *model = context; int64_t *running_topic_counts = reinterpret_cast<int64_t *>( context + model64_size * sizeof(int64_t) / sizeof(int32_t)); int32_t unique_word_count = static_cast<int32_t>(words.size()); for(int32_t it = 0; it < iter_num; it++){ int32_t word_index = topic_num; for(int32_t i = 0; i < unique_word_count; i++) { int32_t wordid = words[i]; for(int32_t j = 0; j < counts[i]; j++){ int32_t topic = doc_topic[word_index]; int32_t retopic = __lda_gibbs_sample( topic_num, topic, doc_topic.ptr(), model + wordid * (topic_num + 1), running_topic_counts, alpha, beta); doc_topic[word_index] = retopic; doc_topic[topic]--; doc_topic[retopic]++; if(iter_num == 1) { if (model[wordid * (topic_num + 1) + retopic] <= 2e9) { running_topic_counts[topic] --; running_topic_counts[retopic] ++; model[wordid * (topic_num + 1) + topic]--; model[wordid * (topic_num + 1) + retopic]++; } else { model[wordid * (topic_num + 1) + topic_num] = 1; } } word_index++; } } } return doc_topic; }
AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words) { //Arguments from stack StringType property = eval(words); AnyType selector = eval(words); AnyType value = eval(words); Simulation * sim = m->GetSimulation(); unsigned char * partsBlock = (unsigned char*)&sim->parts[0]; int returnValue = 0; FormatType propertyFormat; int propertyOffset = GetPropertyOffset(property.Value(), propertyFormat); if(propertyOffset==-1) throw GeneralException("Invalid property"); //Selector int newValue; float newValuef; if (value.GetType() == TypeNumber) { newValue = newValuef = ((NumberType)value).Value(); } else if (value.GetType() == TypeFloat) { newValue = newValuef = ((FloatType)value).Value(); } else if(value.GetType() == TypeString) { if (property.Value() == "temp") { std::string newString = ((StringType)value).Value(); if (newString.at(newString.length()-1) == 'C') newValuef = atof(newString.substr(0, newString.length()-1).c_str())+273.15; else if (newString.at(newString.length()-1) == 'F') newValuef = (atof(newString.substr(0, newString.length()-1).c_str())-32.0f)*5/9+273.15f; else throw GeneralException("Invalid value for assignment"); } else { newValue = GetParticleType(((StringType)value).Value()); if (newValue < 0 || newValue >= PT_NUM) { // TODO: add element CAKE to invalidate this if (!strcasecmp(((StringType)value).Value().c_str(),"cake")) throw GeneralException("Cake is a lie, not an element"); throw GeneralException("Invalid element"); } } } else throw GeneralException("Invalid value for assignment"); if (property.Value() == "type" && (newValue < 0 || newValue >= PT_NUM || !sim->elements[newValue].Enabled)) throw GeneralException("Invalid element"); if(selector.GetType() == TypePoint || selector.GetType() == TypeNumber) { int partIndex = -1; if(selector.GetType() == TypePoint) { ui::Point tempPoint = ((PointType)selector).Value(); if(tempPoint.X<0 || tempPoint.Y<0 || tempPoint.Y >= YRES || tempPoint.X >= XRES) throw GeneralException("Invalid position"); } else partIndex = ((NumberType)selector).Value(); if(partIndex<0 || partIndex>NPART || sim->parts[partIndex].type==0) throw GeneralException("Invalid particle"); switch(propertyFormat) { case FormatInt: *((int*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue; break; case FormatFloat: *((float*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValuef; break; } returnValue = 1; } else if(selector.GetType() == TypeString && ((StringType)selector).Value() == "all") { switch(propertyFormat) { case FormatInt: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type) { returnValue++; *((int*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; } } break; case FormatFloat: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type) { returnValue++; *((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef; } } break; } } else if(selector.GetType() == TypeString || selector.GetType() == TypeNumber) { int type; if(selector.GetType() == TypeNumber) type = ((NumberType)selector).Value(); else if(selector.GetType() == TypeString) type = GetParticleType(((StringType)selector).Value()); if(type<0 || type>=PT_NUM) throw GeneralException("Invalid particle type"); if(type==0) throw GeneralException("Cannot set properties of particles that do not exist"); std::cout << propertyOffset << std::endl; switch(propertyFormat) { case FormatInt: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type == type) { returnValue++; *((int*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; } } break; case FormatFloat: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type == type) { returnValue++; *((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef; } } break; } } else throw GeneralException("Invalid selector"); return NumberType(returnValue); }
AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words) { //Arguments from stack StringType property = eval(words); AnyType selector = eval(words); AnyType value = eval(words); Simulation * sim = m->GetSimulation(); unsigned char * partsBlock = (unsigned char*)&sim->parts[0]; int returnValue = 0; FormatType propertyFormat; int propertyOffset = GetPropertyOffset(property.Value(), propertyFormat); if(propertyOffset==-1) throw GeneralException("Invalid property"); //Selector int newValue; if(value.GetType() == TypeNumber) newValue = ((NumberType)value).Value(); else if(value.GetType() == TypeString) { newValue = GetParticleType(((StringType)value).Value()); if (newValue < 0 || newValue >= PT_NUM) throw GeneralException("Invalid element"); } else throw GeneralException("Invalid value for assignment"); if (property.Value() == "type" && (newValue < 0 || newValue >= PT_NUM)) throw GeneralException("Invalid element"); if(selector.GetType() == TypePoint || selector.GetType() == TypeNumber) { int partIndex = -1; if(selector.GetType() == TypePoint) { ui::Point tempPoint = ((PointType)selector).Value(); if(tempPoint.X<0 || tempPoint.Y<0 || tempPoint.Y >= YRES || tempPoint.X >= XRES) throw GeneralException("Invalid position"); } else partIndex = ((NumberType)selector).Value(); if(partIndex<0 || partIndex>NPART || sim->parts[partIndex].type==0) throw GeneralException("Invalid particle"); switch(propertyFormat) { case FormatInt: *((int*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue; break; case FormatFloat: *((float*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue; break; } returnValue = 1; } else if(selector.GetType() == TypeString && ((StringType)selector).Value() == "all") { switch(propertyFormat) { case FormatInt: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type) { returnValue++; *((int*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; } } break; case FormatFloat: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type) { returnValue++; *((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; } } break; } } else if(selector.GetType() == TypeString || selector.GetType() == TypeNumber) { int type; if(selector.GetType() == TypeNumber) type = ((NumberType)selector).Value(); else if(selector.GetType() == TypeString) type = GetParticleType(((StringType)selector).Value()); if(type<0 || type>=PT_NUM) throw GeneralException("Invalid particle type"); std::cout << propertyOffset << std::endl; switch(propertyFormat) { case FormatInt: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type == type) { returnValue++; *((int*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; } } break; case FormatFloat: { for(int j = 0; j < NPART; j++) if(sim->parts[j].type == type) { returnValue++; *((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; } } break; } } else throw GeneralException("Invalid selector"); return NumberType(returnValue); }
/** * @brief This function learns the topics of words in a document and is the * main step of a Gibbs sampling iteration. The word topic counts and * corpus topic counts are passed to this function in the first call and * then transfered to the rest calls through args.mSysInfo->user_fctx for * efficiency. * @param args[0] The unique words in the documents * @param args[1] The counts of each unique words * @param args[2] The topic counts and topic assignments in the document * @param args[3] The model (word topic counts and corpus topic * counts) * @param args[4] The Dirichlet parameter for per-document topic * multinomial, i.e. alpha * @param args[5] The Dirichlet parameter for per-topic word * multinomial, i.e. beta * @param args[6] The size of vocabulary * @param args[7] The number of topics * @param args[8] The number of iterations (=1:training, >1:prediction) * @return The updated topic counts and topic assignments for * the document **/ AnyType lda_gibbs_sample::run(AnyType & args) { ArrayHandle<int32_t> words = args[0].getAs<ArrayHandle<int32_t> >(); ArrayHandle<int32_t> counts = args[1].getAs<ArrayHandle<int32_t> >(); MutableArrayHandle<int32_t> doc_topic = args[2].getAs<MutableArrayHandle<int32_t> >(); double alpha = args[4].getAs<double>(); double beta = args[5].getAs<double>(); int32_t voc_size = args[6].getAs<int32_t>(); int32_t topic_num = args[7].getAs<int32_t>(); int32_t iter_num = args[8].getAs<int32_t>(); if(alpha <= 0) throw std::invalid_argument("invalid argument - alpha"); if(beta <= 0) throw std::invalid_argument("invalid argument - beta"); if(voc_size <= 0) throw std::invalid_argument( "invalid argument - voc_size"); if(topic_num <= 0) throw std::invalid_argument( "invalid argument - topic_num"); if(iter_num <= 0) throw std::invalid_argument( "invalid argument - iter_num"); if(words.size() != counts.size()) throw std::invalid_argument( "dimensions mismatch: words.size() != counts.size()"); if(__min(words) < 0 || __max(words) >= voc_size) throw std::invalid_argument( "invalid values in words"); if(__min(counts) <= 0) throw std::invalid_argument( "invalid values in counts"); int32_t word_count = __sum(counts); if(doc_topic.size() != (size_t)(word_count + topic_num)) throw std::invalid_argument( "invalid dimension - doc_topic.size() != word_count + topic_num"); if(__min(doc_topic, 0, topic_num) < 0) throw std::invalid_argument("invalid values in topic_count"); if( __min(doc_topic, topic_num, word_count) < 0 || __max(doc_topic, topic_num, word_count) >= topic_num) throw std::invalid_argument( "invalid values in topic_assignment"); if (!args.getUserFuncContext()) { if(args[3].isNull()) throw std::invalid_argument("invalid argument - the model \ parameter should not be null for the first call"); ArrayHandle<int64_t> model = args[3].getAs<ArrayHandle<int64_t> >(); if(model.size() != (size_t)((voc_size + 1) * topic_num)) throw std::invalid_argument( "invalid dimension - model.size() != (voc_size + 1) * topic_num"); if(__min(model) < 0) throw std::invalid_argument("invalid topic counts in model"); int64_t * state = static_cast<int64_t *>( MemoryContextAllocZero( args.getCacheMemoryContext(), model.size() * sizeof(int64_t))); memcpy(state, model.ptr(), model.size() * sizeof(int64_t)); args.setUserFuncContext(state); } int64_t * state = static_cast<int64_t *>(args.getUserFuncContext()); if(NULL == state){ throw std::runtime_error("args.mSysInfo->user_fctx is null"); } int32_t unique_word_count = static_cast<int32_t>(words.size()); for(int32_t it = 0; it < iter_num; it++){ int32_t word_index = topic_num; for(int32_t i = 0; i < unique_word_count; i++) { int32_t wordid = words[i]; for(int32_t j = 0; j < counts[i]; j++){ int32_t topic = doc_topic[word_index]; int32_t retopic = __lda_gibbs_sample( topic_num, topic, doc_topic.ptr(), state + wordid * topic_num, state + voc_size * topic_num, alpha, beta); doc_topic[word_index] = retopic; doc_topic[topic]--; doc_topic[retopic]++; if(iter_num == 1){ state[voc_size * topic_num + topic]--; state[voc_size * topic_num + retopic]++; state[wordid * topic_num + topic]--; state[wordid * topic_num + retopic]++; } word_index++; } } } return doc_topic; }