向量笛卡尔积_如何创建向量的矢量的笛卡尔积?
首先,我将向您展示一个递归版本。
// Cartesion product of vector of vectors
#include
#include
#include
// Types to hold vector-of-ints (Vi) and vector-of-vector-of-ints (Vvi)
typedef std::vector Vi;
typedef std::vector Vvi;
// Just for the sample -- populate the intput data set
Vvi build_input() {
Vvi vvi;
for(int i = 0; i < 3; i++) {
Vi vi;
for(int j = 0; j < 3; j++) {
vi.push_back(i*10+j);
}
vvi.push_back(vi);
}
return vvi;
}
// just for the sample -- print the data sets
std::ostream&
operator<
{
os << "(";
std::copy(vi.begin(), vi.end(), std::ostream_iterator(os, ", "));
os << ")";
return os;
}
std::ostream&
operator<
{
os << "(\n";
for(Vvi::const_iterator it = vvi.begin();
it != vvi.end();
it++) {
os << " " << *it << "\n";
}
os << ")";
return os;
}
// recursive algorithm to to produce cart. prod.
// At any given moment, "me" points to some Vi in the middle of the
// input data set.
// for int i in *me:
// add i to current result
// recurse on next "me"
//
void cart_product(
Vvi& rvvi, // final result
Vi& rvi, // current result
Vvi::const_iterator me, // current input
Vvi::const_iterator end) // final input
{
if(me == end) {
// terminal condition of the recursion. We no longer have
// any input vectors to manipulate. Add the current result (rvi)
// to the total set of results (rvvvi).
rvvi.push_back(rvi);
return;
}
// need an easy name for my vector-of-ints
const Vi& mevi = *me;
for(Vi::const_iterator it = mevi.begin();
it != mevi.end();
it++) {
// final rvi will look like "a, b, c, ME, d, e, f"
// At the moment, rvi already has "a, b, c"
rvi.push_back(*it); // add ME
cart_product(rvvi, rvi, me+1, end); add "d, e, f"
rvi.pop_back(); // clean ME off for next round
}
}
// sample only, to drive the cart_product routine.
int main() {
Vvi input(build_input());
std::cout << input << "\n";
Vvi output;
Vi outputTemp;
cart_product(output, outputTemp, input.begin(), input.end());
std::cout << output << "\n";
}
现在,我将向您展示我无耻地从@John偷走的递归迭代版本:
程序的其余部分几乎相同,只显示了cart_product功能。
// Seems like you'd want a vector of iterators
// which iterate over your individual vectors.
struct Digits {
Vi::const_iterator begin;
Vi::const_iterator end;
Vi::const_iterator me;
};
typedef std::vector Vd;
void cart_product(
Vvi& out, // final result
Vvi& in) // final result
{
Vd vd;
// Start all of the iterators at the beginning.
for(Vvi::const_iterator it = in.begin();
it != in.end();
++it) {
Digits d = {(*it).begin(), (*it).end(), (*it).begin()};
vd.push_back(d);
}
while(1) {
// Construct your first product vector by pulling
// out the element of each vector via the iterator.
Vi result;
for(Vd::const_iterator it = vd.begin();
it != vd.end();
it++) {
result.push_back(*(it->me));
}
out.push_back(result);
// Increment the rightmost one, and repeat.
// When you reach the end, reset that one to the beginning and
// increment the next-to-last one. You can get the "next-to-last"
// iterator by pulling it out of the neighboring element in your
// vector of iterators.
for(Vd::iterator it = vd.begin(); ; ) {
// okay, I started at the left instead. sue me
++(it->me);
if(it->me == it->end) {
if(it+1 == vd.end()) {
// I'm the last digit, and I'm about to roll
return;
} else {
// cascade
it->me = it->begin;
++it;
}
} else {
// normal
break;
}
}
}
}
向量笛卡尔积_如何创建向量的矢量的笛卡尔积?相关推荐
- 词向量与词向量拼接_动态词向量算法—ELMo
传统的词向量模型,例如 Word2Vec 和 Glove 学习得到的词向量是固定不变的,即一个单词只有一种词向量,显然不适合用于多义词.而 ELMo 算法使用了深度双向语言模型 (biLM),只训练语 ...
- 词向量与词向量拼接_「NLP-词向量」一文详述词向量的由来及本质
词嵌入是所有自然语言处理任务所必须要经历的步骤,非常的重要.词向量在网络上已经有了大量的文章,但是,出于我们专栏的完整性系统性的考虑,笔者还是决定加上这样一个专题. 计划用3-4次,彻底说清楚在自然语 ...
- 一个向量图像创建为一个矢量插图程序直线和曲线是连接点
所有的计算机图像分为两种基本类型:光栅图像(也称为一个位图)和矢量图像.在某些情况下,推荐使用矢量图形,在其他人,这取决于项目的性质,我们建议使用一个位图图像.有时,这两种格式可以一起使用.理解的优点 ...
- c++中int向量初始化_以不同的方式在C ++中初始化2D向量
c++中int向量初始化 Prerequisite: Initialize 1D vector 先决条件: 初始化一维向量 Before discussing about the initializa ...
- python二维向量运算模拟_python二维向量运算_[VB.NET][C#]二维向量的基本运算
前言 在数学中,几何向量是指具有大小和方向的几何对象. 在编程中,向量有着广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 第一节 构造函数 通过创建一个二维向量的类(或结构体),实现向量的表 ...
- 向量外积_解析几何 -向量
目录 1.向量 2.内积 3.外积 4.混合积 5.双重外积 6.关系式 正文 1.向量 vector 引入vector O 规定O没有确切的方向,即与任何向量不仅平行,而且垂直. 申明:本文章的向量 ...
- 两个三维向量叉积_线性代数的本质08 叉积
08-1 叉积基本介绍 [熟肉]线性代数的本质 - 08第一部分 - 叉积的标准介绍_哔哩哔哩 (゜-゜)つロ 干杯~-bilibiliwww.bilibili.com 叉积也可以从线性变换的角度来 ...
- 叉乘 线性代数_线性代数4——向量3(叉积、外积、向量积)
什么是叉积 向量的叉积也叫外积.向量积.叉乘或矢量积.两个向量的叉积是这样表示的: ,这种乘法的计算结果是另一个矢量 ,这个矢量 的大小等于原来两个矢量的大小的乘积再乘以两个矢量夹角 (小于180度) ...
- 词向量与词向量拼接_第一节——词向量与ELmo(转)
最近在家听贪心学院的NLP直播课.都是比较基础的内容.放到博客上作为NLP 课程的简单的梳理. 本节课程主要讲解的是词向量和Elmo.核心是Elmo,词向量是基础知识点. Elmo 是2018年提出的 ...
最新文章
- 使用消息来处理多线程程序中的一些问题
- C#_获取 SQL服务器列表
- leetcode-125-Valid Palindrome
- 子模板继承父模板示例_模板设计模式示例
- 相关与卷积、各种误差
- java http的get,post请求
- C#LeetCode刷题之#541-反转字符串 II(Reverse String II)
- 数据分箱4——卡方最优分箱 ChiMerge算法使用(有监督)
- python中pass的使用_pass语句如何在Python项目中使用
- 解决CocosCreator2.3.1使用VideoPlayer加载视频黑屏问题(修改底层代码)
- 大 Θ记号、大 Ω记号、空间复杂度、时间复杂度
- Thinking in Java Chapter 14
- Java后端技术概览
- flv文件修复工具——FLVMDI的使用方法
- java 给图片加水印图片(水印位置与角度可定义)
- resolv.conf文件
- 获取公众号的关注链接
- C# 中的委托和事件1
- QT之 QSQLite
- 如何解决wup.exe文件占用cpu资源
热门文章
- 在Ubuntu 10.10下安装JDK配置Eclipse及Tomcat
- TestInside640-801 v11(神州testv11)题库视频讲解(全部上传完毕)
- 【机器学习】最容易实现的基于OpenCV的人脸检测代码、检测器及检测效果
- MATLAB二维图形坐标变换
- matlab实现图像的高斯滤波
- 高效并发处理之libevent
- deepin--eclipse安装与配置
- 利用python编写设计多线程web服务器(计算机网络_自顶向下第六版_第二章1和4的编程作业)
- python怎么编辑文件夹_python创建修改文件
- 学python什么视频教程_学习python有什么好的视频教程?