Skip to content

Commit 1ffd2a5

Browse files
author
Bowen Fu
committed
RLC / monteCarlo.
1 parent ce14dd7 commit 1ffd2a5

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

stream.cpp

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <cstdlib>
22
#include <optional>
33
#include <iostream>
4+
#include <numeric>
45

56
// This is a must-have for recursive definition of stream.
67
template <typename ValueT>
@@ -533,6 +534,88 @@ auto solve(FuncT f, T y0, T dt)
533534
return *y;
534535
}
535536

537+
template <typename T>
538+
auto RLC(T R, T L, T C, T dt)
539+
{
540+
return [=](T Vc0, T Il0)
541+
{
542+
auto dVc = [=] (auto Il)
543+
{
544+
return scaleStream(Il, -T{1}/C);
545+
};
546+
auto dIl = [=] (auto Vc, auto Il)
547+
{
548+
return addStreams(scaleStream(Vc, T{1}/L), scaleStream(Il, -R/L));
549+
};
550+
auto Vc = std::make_shared<Stream<T>>();
551+
auto Il = std::make_shared<Stream<T>>();
552+
*Vc = integral([=]{ return dVc(*Il); }, Vc0, dt);
553+
*Il = integral([=]{ return dIl(*Vc, *Il); }, Il0, dt);
554+
return std::make_pair(*Vc, *Il);
555+
};
556+
}
557+
558+
template <typename T>
559+
auto randomNumbers(T seed)
560+
{
561+
auto randUpdate = [](T v)
562+
{
563+
return ((v * T{1103515245} + T{12345}) / T{6533}) % T{32174682};
564+
};
565+
566+
auto result = std::make_shared<Stream<T>>();
567+
*result = Stream<T>{seed, [=]{ return streamMap(randUpdate, *result); }};
568+
return *result;
569+
}
570+
571+
template <typename FuncT, typename ValueT>
572+
auto mapSuccessivePairs(FuncT&& func, Stream<ValueT> const& stream) -> Stream<std::invoke_result_t<FuncT, ValueT, ValueT>>
573+
{
574+
auto v1 = *stream.value();
575+
auto next = stream.next();
576+
auto v2 = *next.value();
577+
auto v = func(v1, v2);
578+
return {
579+
v,
580+
[=] { return mapSuccessivePairs(func, next.next()); }
581+
};
582+
}
583+
584+
template <typename T>
585+
auto cesaroStream()
586+
{
587+
return mapSuccessivePairs(
588+
[](T r1, T r2) -> bool { return std::gcd(r1, r2) == 1; },
589+
randomNumbers(T{11}) // random seed
590+
);
591+
}
592+
593+
auto monteCarlo(Stream<bool> const& experimentStream, int32_t passed, int32_t failed) -> Stream<double>
594+
{
595+
auto next = [=](int32_t passed, int32_t failed)
596+
{
597+
return Stream<double>{
598+
double(passed) / (passed + failed),
599+
[=] {
600+
return monteCarlo(experimentStream.next(), passed, failed);
601+
}
602+
};
603+
};
604+
if (*experimentStream.value())
605+
{
606+
return next(passed+1, failed);
607+
}
608+
return next(passed, failed+1);
609+
}
610+
611+
auto pi_ = streamMap(
612+
[](auto p)
613+
{
614+
return sqrt(6.0/p, 1e-6);
615+
},
616+
monteCarlo(cesaroStream<int64_t>(), int64_t{0}, int64_t{0})
617+
);
618+
536619
int32_t main()
537620
{
538621
// auto const even = [](auto&& n) { return n%2 == 0;};
@@ -562,6 +645,19 @@ int32_t main()
562645
// printStream(pairs(integers<int32_t>(), integers<int32_t>()));
563646
// auto RC1 = vOfRC(5.0, 1.0, 0.1);
564647
// printStream(RC1(ones<double>(), 5));
565-
std::cout << streamRef(solve([](auto y) { return y; }, 1.0, 0.001), 1000) << std::endl;
648+
// std::cout << streamRef(solve([](auto y) { return y; }, 1.0, 0.001), 1000) << std::endl;
649+
// auto RLC1 = RLC(1.0, 1.0, 0.2, 0.1);
650+
// auto RLC1_ = RLC1(10.0, 0.0);
651+
/*
652+
import numpy as np
653+
import matplotlib.pyplot as plt
654+
x = np.loadtxt("log")
655+
plt.plot(x[:1000])
656+
plt.savefig("x.png")
657+
*/
658+
// printStream(RLC1_.first);
659+
// printStream(RLC1_.second);
660+
// printStream(cesaroStream<int32_t>());
661+
printStream(pi_);
566662
return 0;
567663
}

0 commit comments

Comments
 (0)