蒙特卡诺近似与PBM
介绍蒙特卡诺近似的例子代码
- #include<fstream>
- #include<iostream>
- #include<cstdlib>
- #include<cstdio>
- #include<string>
- #include<ctime>
- #include<memory>
- using std::ifstream;
- using std::ofstream;
- using std::string;
- using std::cout;
- using std::endl;
- using std::ios;
- using std::unique_ptr;
- double randx()
- {
- return rand() % / (double);
- }
- int main(void)
- {
- ifstream ifs("D:/tex.pbm");
- string header;
- uint32_t w, h, l;
- ifs >> header;
- ifs >> w >> h >> l;
- cout << "w: " << w << " ,h: " << h << " ,l: " << l << endl;
- ifs.ignore();
- unsigned char* pixles = new unsigned char[w * h * ];
- ifs.read((char*)pixles, w * h * );
- int nsamples = ;
- srand(time());
- float avgr = , avgg = , avgb = ;
- float sumr = , sumg = , sumb = ;
- for (int i = ; i < nsamples; i++)
- {
- float x = randx() * w;
- float y = randx() * h;
- int n = ((int)(y * w) + int(x)) * ;
- sumr += pixles[n];
- sumg += pixles[n + ];
- sumb += pixles[n + ];
- }
- sumr /= nsamples;
- sumg /= nsamples;
- sumb /= nsamples;
- for (int i = ; i < h; i++)
- {
- for (int j = ; j < w; j++)
- {
- int n = (i * w + j) * ;
- avgr += pixles[n];
- avgg += pixles[n + ];
- avgb += pixles[n + ];
- }
- }
- avgr /= w * h;
- avgg /= w * h;
- avgb /= w * h;
- printf("Average: %0.2f %0.2f %0.2f \n", avgr, avgg, avgb);
- printf("Aproximation: %0.2f %0.2f %0.2f \n", sumr, sumg, sumb);
- delete pixles;
- system("pause");
- return ;
- }
蒙特卡诺近似与PBM的更多相关文章
- R语言与正态性检验
1.Kolmogorov-Smirnov正态性检验 Kolmogorov-Smirnov是比较一个频率分布f(x)与理论分布g(x)或者两个观测值分布的检验方法,若两者间的差距很小,则推论该样本取自某 ...
- 蒙特罗卡π算法(C++语言描述)
圆的面积计算公式为:S=π*r*r 将圆放到一个直角坐标系中,如图黄色部分的面积是S/4=(π*r*r)/4;如果我们将取一个单位圆,则S/4=π/4. 因为是单位圆,半径为1,所以图中红色正方形的面 ...
- WPF弹出带蒙板的消息框
效果图 思路 拿到父级窗体的内容,放入一个容器里,再在容器里放入一个半透明层.将整个容器赋给父级窗体的内容. 关闭时反向操作. 代码 消息窗弹出时 /// <summary> /// 弹出 ...
- 设置 tableview 的背景颜色,总是有蒙层
1.给tableview添加了背景图片后, cell 总是有一层蒙层蒙着,很阴暗. 2.实验以后才发现背景图片被放在了 cell 的后面.
- 长文丨papi、咪蒙、罗胖之后,内容创业的机会在哪儿
一.内容的一年 app死了,内容永生! 2016年,创业圈画风突变,昨天还在激辩java和PHP谁更好的技术宅们.还在梦想着成为第二个乔布斯改变世界的产品狗们.还在忽悠着用O2O横扫传统行业的小老板们 ...
- Replication的犄角旮旯(六)-- 一个DDL引发的血案(上)(如何近似估算DDL操作进度)
<Replication的犄角旮旯>系列导读 Replication的犄角旮旯(一)--变更订阅端表名的应用场景 Replication的犄角旮旯(二)--寻找订阅端丢失的记录 Repli ...
- 蒙特·卡罗算法的Python实现
一 背景 此算法诞生的背景是: 曼哈顿计划,有极大的计算需求. 计算机刚开始发展,最适合做计算. 蒙特卡洛算法理论基础是概率论,实际就是暴力计算逼近理想结果.正是在以上两个背景下,它刚好得到了极大的应 ...
- PRML读书会第十章 Approximate Inference(近似推断,变分推断,KL散度,平均场, Mean Field )
主讲人 戴玮 (新浪微博: @戴玮_CASIA) Wilbur_中博(1954123) 20:02:04 我们在前面看到,概率推断的核心任务就是计算某分布下的某个函数的期望.或者计算边缘概率分布.条件 ...
- CSU 1328: 近似回文词
省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1 ...
随机推荐
- error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler op
caffe c++11编译问题 问题:error: #error This file requires compiler and library support for the ISO C++ 201 ...
- jsp篇 之 EL表达式
EL表达式: 形式:${ } 作用:从一个[范围对象里面取值]或者从一个对象中取值或是向页面输出值. 之前我们使用<% ... %>等形式,并不够简洁. 例如: <% request ...
- 进程初识和multiprocessing模块之Process
一.什么是进程 进程就是运行中的程序 进程是操作系统中最小的资源分配单位 进程与进程之间的关系 : 数据隔离的 进程的id:Process id = pid pid是一个全系统唯一的对某个进程的标识, ...
- java编译过程(字节码编译和即时编译)
Javac编译与JIT编译 简介: 编译包括两种情况: 1,源码编译成字节码 2,字节码编译成本地机器码(符合本地系统专属的指令) 解释执行也包括两种情况: 1,源码解释执行 2,字节码解释执行 解释 ...
- 初识并发编程 MPI
MPI是一个跨语言的通讯协议,用于并发编程.MPI标准定义了一组具有可移植性的编程接口. 安装环境 MPICH 是开源的消息传递接口(MPI)标准的实现. 下载地址 # 解压文件 tar -xzvf ...
- hdu 3480 Division(四边形不等式优化)
Problem Description Little D is really interested in the theorem of sets recently. There’s a problem ...
- yii2 redirect重定向
redirect使用方法 $this->redirect(array('/site/contact','id'=>12)); //http://www.kuitao8.com/testwe ...
- 【gdoi2018 day2】第二题 滑稽子图(subgraph)(性质DP+多项式)
题目大意 [gdoi2018 day2]第二题 滑稽子图(subgraph) 给你一颗树\(T\),以及一个常数\(K\),对于\(T\)的点集\(V\)的子集\(S\). 定义\(f(S)\)为点集 ...
- MySQL_列值为null对索引的影响_实践
一.首先看一个我在某公众号看到的一个关于数据库优化的举措 二.如果where子句中查询的列执行了 “is null” 或者 “is not null” 或者 “<=> null” 会不会使 ...
- Visual Studio图形调试器详细使用教程(基于DirectX11)
前言 对于DirectX程序开发者来说,学会使用Visual Studio Graphics Debugger(图形调试器)可以帮助你全面了解渲染管线绑定的资源和运行状态,从而确认问题所在.现在就以我 ...