double Point::distance(Point const& start, Point const& ctrl, Point const& end) const noexcept { const Point A = ctrl - start; const Point B = start - ctrl * 2 - end; const Point C = start - *this; double sol1, sol2, sol3; const ulong nresult = solve(B.length(), 3 * A.dot(B), 2 * A.length() + C.dot(B), A.dot(C), sol1, sol2, sol3); if(nresult) { double dist = distance(fromLine(start, ctrl, end, sol1)); if(nresult > 1) { const double dist2 = distance(fromLine(start, ctrl, end, sol2)); if(dist2 < dist) { dist = dist2; } } if(nresult > 2) { const double dist2 = distance(fromLine(start, ctrl, end, sol3)); if(dist2 < dist) { dist = dist2; } } return dist; } else { return min(distance(start), distance(end)); } }
Point Point::nearest(Point const& start, Point const& ctrl, Point const& end) const noexcept { const Point A = ctrl - start; const Point B = start - ctrl * 2 - end; const Point C = start - *this; double sol1, sol2, sol3; const ulong nresult = solve(B.length(), 3 * A.dot(B), 2 * A.length() + C.dot(B), A.dot(C), sol1, sol2, sol3); if(nresult) { Point pt = fromLine(start, ctrl, end, sol1); double dist = distance(pt); if(nresult > 1) { const Point pt2 = fromLine(start, ctrl, end, sol2); const double dist2 = distance(pt2); if(dist2 < dist) { dist = dist2; pt = pt2; } } if(nresult > 2) { const Point pt2 = fromLine(start, ctrl, end, sol3); const double dist2 = distance(pt2); if(dist2 < dist) { dist = dist2; pt = pt2; } } return pt; } else { return (distance(start) < distance(end)) ? start : end; } }
int checkDiagonal(){ int i = 0, colFrom = 0, colTo = 0, R = 0; D = -1; for(; i < N && N - i > D; i++){ colFrom = fromLine(i); colTo = fromDiag(colFrom); R = colTo - colFrom; if(R > D) D = R; } return D + 1; }
Point Point::nearest(Point const& start, Point const& ctrl1, Point const& ctrl2, Point const& end) const noexcept { array<Point, 6> W = {Point(0., 0.), Point(0.2, 0.), Point(0.4, 0.), Point(0.6, 0.), Point(0.8, 0.), Point(1., 0.)}; array<Point, 4> C = {Point(start - *this), Point(ctrl1 - *this), Point(ctrl2 - *this), Point(end - *this)}; array<Point, 3> D = {Point((ctrl1 - start) * 3.), Point((ctrl2 - ctrl1) * 3.), Point((end - ctrl2) * 3.)}; static const double z[3][4] ={{1.0, 0.6, 0.3, 0.1}, {0.4, 0.6, 0.6, 0.4}, {0.1, 0.3, 0.6, 1.0}}; double cd[3][4]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) { cd[i][j] = D[i].dot(C[j]); } } for(int k = 0; k < 6; k++) { for(int i = max(0, k - 2); i <= min(k, 3); i++) { W[k].y(W[k].y() + cd[k - i][i] * z[k - i][i]); } } double t_candidate[5]; ulong n_solutions = solve(W, t_candidate, 0ul); Point pt = end; double dist = distance(end); for(int i = 0; i < n_solutions; i++) { const Point pt2 = fromLine(start, ctrl1, ctrl2, end, t_candidate[i]); const double new_dist = distance(pt2); if(new_dist < dist) { dist = new_dist; pt = pt2; } } return pt; }
nsresult nsEudoraMailbox::ImportMessage( SimpleBufferTonyRCopiedOnce &headers, SimpleBufferTonyRCopiedOnce &body, nsCString& defaultDate, nsAutoCString& bodyType, nsIOutputStream *pDst, int32_t *pMsgCount) { nsresult rv = NS_OK; uint32_t written = 0; nsEudoraCompose compose; // Unfortunately Eudora stores HTML messages in the sent folder // without any content type header at all. If the first line of the message body is <html> // then mark the message as html internally...See Bug #258489 if (body.m_pBuffer && (body.m_writeOffset > (int32_t)strlen(kHTMLTag)) && (strncmp(body.m_pBuffer, kHTMLTag, strlen(kHTMLTag)) == 0)) bodyType = "text/html"; // ignore whatever body type we were given...force html compose.SetBody(body.m_pBuffer, body.m_writeOffset - 1, bodyType); compose.SetHeaders(headers.m_pBuffer, headers.m_writeOffset - 1); compose.SetAttachments(&m_attachments); compose.SetDefaultDate(defaultDate); nsCOMPtr <nsIFile> compositionFile; rv = compose.SendTheMessage(m_mailImportLocation, getter_AddRefs(compositionFile)); if (NS_SUCCEEDED(rv)) { nsCString fromLine(eudoraFromLine); SimpleBufferTonyRCopiedOnce copy; copy.Allocate(kCopyBufferSize); /* IMPORT_LOG0("Composed message in file: "); DUMP_FILENAME(compositionFile, true); */ // copy the resulting file into the destination file! rv = compose.CopyComposedMessage(fromLine, compositionFile, pDst, copy); DeleteFile(compositionFile); if (NS_FAILED(rv)) IMPORT_LOG0("*** Error copying composed message to destination mailbox\n"); if (pMsgCount) (*pMsgCount)++; } else { IMPORT_LOG0("*** Error composing message, writing raw message\n"); rv = WriteFromSep(pDst); rv = pDst->Write(kComposeErrorStr, strlen(kComposeErrorStr), &written); if (NS_SUCCEEDED(rv)) rv = pDst->Write(headers.m_pBuffer, headers.m_writeOffset - 1, &written); if (NS_SUCCEEDED(rv) && (written == (headers.m_writeOffset - 1))) rv = pDst->Write("\x0D\x0A" "\x0D\x0A", 4, &written); if (NS_SUCCEEDED(rv) && (written == 4)) rv = pDst->Write(body.m_pBuffer, body.m_writeOffset - 1, &written); if (NS_SUCCEEDED(rv) && (written == (body.m_writeOffset - 1))) { rv = pDst->Write("\x0D\x0A", 2, &written); if (written != 2) rv = NS_ERROR_FAILURE; } if (NS_FAILED(rv)) IMPORT_LOG0("*** Error writing to destination mailbox\n"); } return rv; }
int main() { std::mt19937 generator(time(nullptr)); std::vector<std::vector<double>> timeSeries; std::ifstream fromFile("resources/data.txt"); if (!fromFile.is_open()) { std::cerr << "Could not open data.txt!" << std::endl; return 1; } // Skip first line std::string line; std::getline(fromFile, line); int numEntries = 1; for (int i = 0; i < line.size(); i++) if (line[i] == ',') numEntries++; int numSkipEntries = 1; int numEntriesUse = numEntries - numSkipEntries; std::vector<double> minimums(numEntriesUse, 999999999.0); std::vector<double> maximums(numEntriesUse, -999999999.0); while (fromFile.good() && !fromFile.eof()) { std::vector<double> entries(numEntriesUse); std::string line; std::getline(fromFile, line); std::istringstream fromLine(line); std::string param; // Skip entries for (int i = 0; i < numSkipEntries; i++) { std::string entry; std::getline(fromLine, entry, ','); } for (int i = 0; i < numEntriesUse; i++) { std::string entry; std::getline(fromLine, entry, ','); if (entry == "") entries[i] = 0.0; else { double value = std::stod(entry); maximums[i] = std::max(maximums[i], value); minimums[i] = std::min(minimums[i], value); entries[i] = value; } } timeSeries.push_back(entries); } // Rescale for (int i = 0; i < timeSeries.size(); i++) { for (int j = 0; j < timeSeries[i].size(); j++) { timeSeries[i][j] = (timeSeries[i][j] - minimums[j]) / std::max(0.0001, (maximums[j] - minimums[j])); } } /*timeSeries.clear(); timeSeries.resize(10); timeSeries[0] = { 0.0f, 1.0f, 0.0f }; timeSeries[1] = { 0.0f, 0.0f, 0.0f }; timeSeries[2] = { 1.0f, 1.0f, 0.0f }; timeSeries[3] = { 0.0f, 0.0f, 1.0f }; timeSeries[4] = { 0.0f, 1.0f, 0.0f }; timeSeries[5] = { 0.0f, 0.0f, 1.0f }; timeSeries[6] = { 0.0f, 0.0f, 0.0f }; timeSeries[7] = { 0.0f, 0.0f, 0.0f }; timeSeries[8] = { 0.0f, 1.0f, 0.0f }; timeSeries[9] = { 0.0f, 1.0f, 1.0f };*/ std::vector<sdr::IPredictiveRSDR::LayerDesc> layerDescs(3); layerDescs[0]._width = 8; layerDescs[0]._height = 8; layerDescs[1]._width = 6; layerDescs[1]._height = 6; layerDescs[2]._width = 4; layerDescs[2]._height = 4; sdr::IPredictiveRSDR prsdr; prsdr.createRandom(4, 5, 8, layerDescs, -0.01f, 0.01f, 0.0f, generator); float avgError = 1.0f; float avgErrorDecay = 0.01f; for (int iter = 0; iter < 1000; iter++) { for (int i = 0; i < timeSeries.size(); i++) { float error = 0.0f; for (int j = 0; j < timeSeries[i].size(); j++) { error += std::pow(prsdr.getPrediction(j) - timeSeries[i][j], 2); prsdr.setInput(j, timeSeries[i][j]); } avgError = (1.0f - avgErrorDecay) * avgError + avgErrorDecay * error; prsdr.simStep(generator); if (i % 10 == 0) { std::cout << "Iteration " << i << ": " << avgError << std::endl; } } } return 0; }
nsresult nsEudoraMailbox::ImportMailbox( PRUint32 *pBytes, PRBool *pAbort, const PRUnichar *pName, nsIFile *pSrc, nsIFile *pDst, PRInt32 *pMsgCount) { nsCOMPtr<nsIFile> tocFile; nsCOMPtr <nsIInputStream> srcInputStream; nsCOMPtr <nsIInputStream> tocInputStream; nsCOMPtr <nsIOutputStream> mailOutputStream; PRBool importWithoutToc = PR_TRUE; PRBool deleteToc = PR_FALSE; nsresult rv; nsCOMPtr<nsIFile> mailFile; if (pMsgCount) *pMsgCount = 0; rv = pSrc->GetFileSize( &m_mailSize); rv = NS_NewLocalFileInputStream(getter_AddRefs(srcInputStream), pSrc); if (NS_FAILED( rv)) return( rv); NS_ADDREF( pSrc); // First, get the index file for this mailbox rv = FindTOCFile( pSrc, getter_AddRefs( tocFile), &deleteToc); if (NS_SUCCEEDED( rv) && tocFile) { IMPORT_LOG0( "Reading euroda toc file: "); DUMP_FILENAME( tocFile, PR_TRUE); rv = NS_NewLocalFileOutputStream(getter_AddRefs(mailOutputStream), pDst); NS_ENSURE_SUCCESS(rv, rv); // Read the toc and import the messages rv = ImportMailboxUsingTOC( pBytes, pAbort, srcInputStream, tocFile, mailOutputStream, pMsgCount); // clean up if (deleteToc) DeleteFile( tocFile); // If we were able to import with the TOC, then we don't need to bother // importing without the TOC. if ( NS_SUCCEEDED(rv) ) { importWithoutToc = PR_FALSE; IMPORT_LOG0( "Imported mailbox: "); DUMP_FILENAME( pSrc, PR_FALSE); IMPORT_LOG0( " Using TOC: "); DUMP_FILENAME(tocFile, PR_TRUE); } else { IMPORT_LOG0( "*** Error importing with TOC - will import without TOC.\n"); } } // pSrc must be Released before returning if (importWithoutToc) { // The source file contains partially constructed mail messages, // and attachments. We should first investigate if we can use the mailnews msgCompose // stuff to do the work for us. If not we have to scan the mailboxes and do TONS // of work to properly reconstruct the message - Eudora is so nice that it strips things // like MIME headers, character encoding, and attachments - beautiful! rv = pSrc->GetFileSize( &m_mailSize); SimpleBufferTonyRCopiedOnce readBuffer; SimpleBufferTonyRCopiedOnce headers; SimpleBufferTonyRCopiedOnce body; SimpleBufferTonyRCopiedOnce copy; nsCString fromLine(eudoraFromLine); headers.m_convertCRs = PR_TRUE; body.m_convertCRs = PR_TRUE; copy.Allocate( kCopyBufferSize); readBuffer.Allocate( kMailReadBufferSize); ReadFileState state; state.offset = 0; state.size = m_mailSize; state.pFile = pSrc; IMPORT_LOG0( "Reading mailbox\n"); if (NS_SUCCEEDED( rv )) { nsCString defaultDate; nsCAutoString bodyType; IMPORT_LOG0( "Reading first message\n"); while (!*pAbort && NS_SUCCEEDED( rv = ReadNextMessage( &state, readBuffer, headers, body, defaultDate, bodyType, NULL))) { if (pBytes) { *pBytes += body.m_writeOffset - 1 + headers.m_writeOffset - 1; } rv = ImportMessage(headers, body, defaultDate, bodyType, mailOutputStream, pMsgCount); if (!readBuffer.m_bytesInBuf && (state.offset >= state.size)) break; } } else { IMPORT_LOG0( "*** Error creating file spec for composition\n"); } } pSrc->Release(); return( rv); }