BOOL CPreviewDC::ExtTextOut(int x, int y, UINT nOptions, LPCRECT lpRect, LPCTSTR lpszString, UINT nCount, LPINT lpDxWidths) { ASSERT(m_hDC != NULL); ASSERT(m_hAttribDC != NULL); ASSERT(lpszString != NULL); ASSERT(lpDxWidths == NULL || AfxIsValidAddress(lpDxWidths, sizeof(int) * nCount, FALSE)); ASSERT(AfxIsValidAddress(lpszString, nCount, FALSE)); int* pDeltas = NULL; LPTSTR pOutputString = NULL; int nRightFixup = 0; if (lpDxWidths == NULL) { if (nCount == 0) // Do nothing return TRUE; TRY { pDeltas = new int[nCount]; pOutputString = new TCHAR[nCount]; } CATCH_ALL(e) { delete[] pDeltas; // in case it was allocated // Note: DELETE_EXCEPTION(e) not required return FALSE; // Could not allocate buffer, cannot display } END_CATCH_ALL ComputeDeltas(x, (LPTSTR)lpszString, nCount, FALSE, 0, NULL, 0, pOutputString, pDeltas, nRightFixup); lpDxWidths = pDeltas; lpszString = pOutputString; } BOOL bSuccess = ::ExtTextOut(m_hDC, x, y, nOptions, lpRect, lpszString, nCount, lpDxWidths); if (nRightFixup != 0 && bSuccess && (GetTextAlign() & TA_UPDATECP)) { CPoint pt; ::GetCurrentPositionEx(m_hDC, &pt); MoveTo(pt.x - nRightFixup, pt.y); } delete[] pDeltas; delete[] pOutputString; return bSuccess; }
CSize CPreviewDC::TabbedTextOut(int x, int y, LPCTSTR lpszString, int nCount, int nTabPositions, LPINT lpnTabStopPositions, int nTabOrigin) { ASSERT(m_hAttribDC != NULL); ASSERT(m_hDC != NULL); ASSERT(lpszString != NULL); ASSERT(AfxIsValidAddress(lpszString, nCount)); ASSERT(lpnTabStopPositions == NULL || AfxIsValidAddress(lpnTabStopPositions, sizeof(int) * nTabPositions, FALSE)); if (nCount <= 0) return 0; // nCount is zero, there is nothing to print int* pDeltas = NULL; LPTSTR pOutputString = NULL; int nRightFixup; TRY { pDeltas = new int[nCount]; pOutputString = new TCHAR[nCount]; } CATCH_ALL(e) { delete[] pDeltas; // Note: DELETE_EXCEPTION(e) not required return 0; // signify error } END_CATCH_ALL UINT uCount = nCount; CSize sizeFinalExtent = ComputeDeltas(x, lpszString, uCount, TRUE, nTabPositions, lpnTabStopPositions, nTabOrigin, pOutputString, pDeltas, nRightFixup); BOOL bSuccess = ExtTextOut(x, y, 0, NULL, pOutputString, uCount, pDeltas); delete[] pDeltas; delete[] pOutputString; if (bSuccess && (GetTextAlign() & TA_UPDATECP)) { CPoint pt; ::GetCurrentPositionEx(m_hDC, &pt); MoveTo(pt.x - nRightFixup, pt.y); } return sizeFinalExtent; }
template <class T> double GaussNewton<T>::Fit(DArray &c) { double sum = 0.; DArray res(x_.size(), 0.); DMatrix jac(x_.size(), t_.GetC()), dc(1, t_.GetC()); do { //break; ComputeResiduals(res); ComputeJacobian(jac); ComputeDeltas(jac, res, dc); cerr << "Deltas: " << endl; PrintMatrix(dc); } while(RoundToZero(UpdateParameters(dc, c)) != 0.); ComputeResiduals(res); for(int i = 0; i < res.size(); ++i) { sum += res[i] * res[i]; } return sqrt(sum); }
Observations MultivariateModel ::SimulateData(io::DataSettings &DS) { /// This function simulates observations (Patients and their measurements y_ij at different time points t_ij) /// according to the model, with a given noise level e_ij, such that y_ij = f(t_ij) + e_ij /// Their is two option: /// 1) The model is not initialized (neither random variables of number of realizations) and it has to be /// This case corresponds to simulated data used to test the model, meaning if it can recover the random variables /// used to simulate the data /// 2) The model is already initialized, thus it should rely on its current state (random variables, realizations, ...) /// This case corresponds to new data, for a dataset augmentation for instance // TODO : /// PITFALL : As for now, only the first option is implemented /// PITFALL2 : Take a closer look at / merge with InitializeFakeRandomVariables /// Initialize the model m_ManifoldDimension = DS.GetCognitiveScoresDimension(); m_NumberOfSubjects = DS.GetNumberOfSimulatedSubjects(); m_RealizationsPerRandomVariable["G"] = 1; for(size_t i = 1; i < m_ManifoldDimension; ++i) m_RealizationsPerRandomVariable["Delta#" + std::to_string(i)] = 1; for(size_t i = 0; i < m_NbIndependentSources*(m_ManifoldDimension - 1); ++i) m_RealizationsPerRandomVariable["Beta#" + std::to_string(i)] = 1; m_RealizationsPerRandomVariable["Ksi"] = m_NumberOfSubjects; m_RealizationsPerRandomVariable["Tau"] = m_NumberOfSubjects; for(int i = 0; i < m_NbIndependentSources; ++i) m_RealizationsPerRandomVariable["S#" + std::to_string(i)] = m_NumberOfSubjects; auto R = SimulateRealizations(); /// Update the model m_G = exp(R.at("G", 0)); ComputeDeltas(R); ComputeOrthonormalBasis(); ComputeAMatrix(R); ComputeSpaceShifts(R); ComputeBlock(R); /// Simulate the data std::random_device RD; std::mt19937 RNG(RD()); std::uniform_int_distribution<int> Uni(DS.GetMinimumNumberOfObservations(), DS.GetMaximumNumberOfObservations()); UniformRandomVariable NumberOfTimePoints(60, 95); GaussianRandomVariable Noise(0, m_Noise->GetVariance()); /// Simulate the data Observations Obs; for(int i = 0; i < m_NumberOfSubjects; ++i) { /// Get a random number of timepoints and sort them VectorType T = NumberOfTimePoints.Samples(Uni(RNG)); T.sort(); m_SubjectTimePoints.push_back(T); /// Simulate the data base on the time-points IndividualObservations IO(T); std::vector<VectorType> Landmarks; for(size_t j = 0; j < T.size(); ++j) { Landmarks.push_back(ComputeParallelCurve(i, j) + Noise.Samples(m_ManifoldDimension)); } IO.AddLandmarks(Landmarks); Obs.AddIndividualData(IO); } /// Initialize the observation and model attributes Obs.InitializeGlobalAttributes(); m_IndividualObservationDate = Obs.GetObservations(); m_SumObservations = Obs.GetTotalSumOfLandmarks(); m_NbTotalOfObservations = Obs.GetTotalNumberOfObservations(); return Obs; }
void MultivariateModel ::UpdateModel(const Realizations &R, int Type, const std::vector<std::string, std::allocator<std::string>> Names) { /// Given a list of names (which in fact corresponds to the variables that have potentially changed), /// the function updates the parameters associated to these names /// Possible parameters to update, depending on the name being in "vect<> Names" bool ComputeG = false; bool ComputeDelta = false; bool ComputeBasis = false; bool ComputeA = false; bool ComputeSpaceShift = false; bool ComputeBlock_ = false; bool IndividualOnly = (Type > -1); /// Parameters to update, depending on the names called for(auto it = Names.begin(); it != Names.end(); ++it) { std::string Name = it->substr(0, it->find_first_of("#")); if(Name == "None") { continue; } else if(Name == "Ksi" or Name == "Tau") { continue; } else if("G" == Name) { IndividualOnly = false; ComputeBasis = true; ComputeA = true; ComputeSpaceShift = true; ComputeBlock_ = true; } else if("Delta" == Name) { IndividualOnly = false; ComputeBasis = true; ComputeA = true; ComputeSpaceShift = true; ComputeBlock_ = true; } else if("Beta" == Name) { IndividualOnly = false; ComputeA = true; ComputeSpaceShift = true; } else if("S" == Name) { ComputeSpaceShift = true; } else if("All" == Name) { ComputeSubjectTimePoint(R, -1); IndividualOnly = false; ComputeG = true; ComputeDelta = true; ComputeBasis = true; ComputeA = true; ComputeSpaceShift = true; ComputeBlock_ = true; } else { std::cerr << "The realization does not exist in the multivariate model > update model" << std::endl; } } // TODO : To parse it even faster, update just the coordinates within the names if(IndividualOnly) ComputeSubjectTimePoint(R, Type); if(ComputeG) m_G = exp(R.at("G", 0)); if(ComputeDelta) ComputeDeltas(R); if(ComputeBasis) ComputeOrthonormalBasis(); if(ComputeA) ComputeAMatrix(R); if(ComputeSpaceShift) ComputeSpaceShifts(R); if(ComputeBlock_) ComputeBlock(R); }