给定unicode 表的这个区域,例如:
...
U+1D44E Dec:119886 MATHEMATICAL ITALIC SMALL A 𝑎
U+1D44F Dec:119887 MATHEMATICAL ITALIC SMALL B 𝑏
U+1D450 Dec:119888 MATHEMATICAL ITALIC SMALL C 𝑐
U+1D451 Dec:119889 MATHEMATICAL ITALIC SMALL D 𝑑
U+1D452 Dec:119890 MATHEMATICAL ITALIC SMALL E 𝑒
U+1D453 Dec:119891 MATHEMATICAL ITALIC SMALL F 𝑓
U+1D454 Dec:119892 MATHEMATICAL ITALIC SMALL G 𝑔
U+1D456 Dec:119894 MATHEMATICAL ITALIC SMALL I 𝑖 # what?!
U+1D457 Dec:119895 MATHEMATICAL ITALIC SMALL J 𝑗
U+1D458 Dec:119896 MATHEMATICAL ITALIC SMALL K 𝑘
U+1D459 Dec:119897 MATHEMATICAL …
Run Code Online (Sandbox Code Playgroud) 我想知道为什么我找不到沿着复杂张量/数组/矩阵的任何维度映射自定义pytorch或numpy转换的实用程序。
我想我记得在 R 中可以使用这样的东西。使用这个 Fantasytch.map
实用程序,您可以执行以下操作:
>>> import torch as tch # or numpy
>>> # one torch tensor
>>> a = tch.tensor([0, 1, 2, 3, 4])
>>> # one torch function (dummy) returning 2 values
>>> f = lambda x: tch.tensor((x + 1, x * 2))
>>> # map f along dimension 0 of a, expecting 2 outputs
>>> res = tch.map(f, a, 0, 2) # fantasy, optimized on CPU/GPU..
>>> res
tensor([[1, 0], …
Run Code Online (Sandbox Code Playgroud) 我的脚本的一个关键部分依赖于大量固定长度字符串的串联。所以我想使用低级numpy.char.join
函数而不是经典的 python build str.join
。
但是,我无法让它正常工作:
import numpy as np
# Example array.
array = np.array([
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
], dtype='<U1')
# Now I wish to get:
# array(['abc', 'def', 'ghi'], dtype='<U3')
# But none of these is successful :(
np.char.join('', array)
np.char.join('', array.astype('<U3'))
np.char.join(np.array(''), array.astype('<U3'))
np.char.join(np.array('').astype('<U3'), array.astype('<U3'))
np.char.join(np.array(['', '', '']).astype('<U3'), array.astype('<U3'))
np.char.join(np.char.asarray(['', '', '']).astype('<U3'), np.char.asarray(array))
np.char.asarray(['', '', '']).join(array)
np.char.asarray(['', '', '']).astype('<U3').join(array.astype('<U3'))
Run Code Online (Sandbox Code Playgroud)
..我的初始数组始终保持不变。
我在这里缺少什么?
numpy 连接大型二维<U1
数组的每一行最有效的方法是什么?
[编辑]:由于性能是一个问题,我对建议的解决方案进行了基准测试。但我还是不知道如何np.char.join
正确调用。
import …
Run Code Online (Sandbox Code Playgroud) 我在这篇文章中读到,a的初始容量std::vector
无法由其构造函数控制.设置它的最佳方法,如果你知道它的大小在运行时将是常量,那么似乎是:
const int size;
std::vector<T> v;
v.reserve(size);
Run Code Online (Sandbox Code Playgroud)
但是,既然T
是一个大类,我很乐意使用构造函数的初始化参数std::vector<T> v(size, T());
.那么只分配我需要的内存而不必手动迭代元素来初始化它们的最佳方法是什么?
std::vector<T> v;
v.reserve(size);
for(T t : v) t = T(); // `has to call T::operator= (that may not even be defined)
Run Code Online (Sandbox Code Playgroud)
要么
std::vector<T> v(size, T()); // may allocate more memory than I need..
v.shrink_to_fit(); // ..then deallocate it immediately (pointless)
Run Code Online (Sandbox Code Playgroud)
什么是最接近我的理想:
std::vector<T> v(size, T(), /*capacity =*/ size);
Run Code Online (Sandbox Code Playgroud)
[编辑]:根据你的答案,我需要更准确的是v
用每个构建的size
实例填充默认构造函数,而不是复制.我怎么能这样做,因为在编译时不知道我不能使用初始化列表?T
T
size
奖励:顺便说一下,为什么没有办法选择矢量的初始容量?
从:help while
文档中可以看出:
NOTE: The ":append" and ":insert" commands don't work
properly inside a ":while" and ":for" loop.
Run Code Online (Sandbox Code Playgroud)
我可以证实他们没有.但是,我应该使用什么来从循环内插入文本?
我class Dad
提供了一些protected
孩子需要的方法,即使它实际上并不知道这些孩子是谁.
class Dad
{
protected:
void method()
{
// some amazing stuff (I swear)
};
};
Run Code Online (Sandbox Code Playgroud)
实际的继承class Child: public Dad
,在当前实现我的计划,已决定将自己推断为几类class GrandKid1: Child
,class GrandKid2: Child
等等.
但是,为了安全和组织起见,Child
更喜欢孙子method()
们不能自己打电话.我如何阻止他们这样做?
显然,以下天真代码会产生链接器错误:
class Child: public Dad
{
private:
void method();
};
Run Code Online (Sandbox Code Playgroud)
如何Child
阻止protected
成员传播method()
到自己的派生类?
简而言之:
Derived
继承自 Base
Holder
模板化为包含指向任何内容的指针我可以说一个对象知道Holder<Base>
是一个Holder<Derived>
.
我怎么能告诉我的编译器?
这不编译:
struct Base { };
struct Derived : Base { };
template <typename T>
struct Holder {
T* point;
int id;
};
Derived* d = new Derived();
Holder<Base> holder {d, 12};
Holder<Derived> specific( static_cast<Holder<Derived>>(holder) );
Run Code Online (Sandbox Code Playgroud)
error: no matching conversion for static_cast from 'Holder<Base>' to 'Holder<Derived>'
这肯定是天真的尝试。但是为什么这不起作用,我应该如何获得specific
我需要的持有人?
Numpy 为任何RxR -> R
函数(如np.multiply.outer
或 )提供优化的外部操作np.subtract.outer
,其行为如下:
>>> np.subtract.outer([6, 5, 4], [3, 2, 1])
array([[3, 4, 5],
[2, 3, 4],
[1, 2, 3]])
Run Code Online (Sandbox Code Playgroud)
Pytorch 似乎没有提供这样的功能(或者我错过了)。
使用火炬张量最好/通常/最快/最干净的方法是什么?
这些天我的代码中发生了一些奇怪的事情.每次我必须构建一个新的成员函数,或者每次我回到之前定义的成员函数时,我认为:
"嘿,无论这个类的哪个实例调用它,这都是完全相同的过程,所以让它变得静止!"
而且我觉得它会减少内存消耗,因为每个实例都不必"携带"一个函数的实现,这是正确的吗?
所以不要像以下那样:
class Foo
{
int _attributes;
double methods(int parameters)
{
// do stuff using..
_attributes;
};
};
Run Code Online (Sandbox Code Playgroud)
我最终得到的结果如下:
class Foo
{
int _attributes;
static double methods(int parameters, Foo* instance)
{
// do stuff using..
instance->_attributes;
};
};
Run Code Online (Sandbox Code Playgroud)
而且我看不到任何不会以这种方式改变的功能.我的所有功能都转向静态,我觉得某处出了点问题.
我错过了什么?再次使用非静态方法有什么用?XD
我开始深入研究BGL,它看起来很棒,但我发现它很难使用。
当我研究这个图表示例时,事情开始变得更加清晰。但我现在面临一个问题:删除顶点后,无法删除图中的边。到目前为止,这似乎使我对 BGL 的理解无效:(
这是我必须使问题清楚地出现的最短代码。
(我用整数 [1..5] 标识顶点,用字母 [a..g] 标识边)
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
using namespace boost;
//==============================================================
// TL;DR : Skip to the end of `main()` where the real problem is
//==============================================================
// lazy iterate over the graph: // {{{
#define VERTEXITERATE(nameV, graph, ...) \
{ \
auto GRAPHITERATOR(vertices(graph)); \
auto& nameV(GRAPHITERATOR.first); \
for(; GRAPHITERATOR.first != GRAPHITERATOR.second; ++GRAPHITERATOR.first) {\
__VA_ARGS__ \
} \
}
#define EDGEITERATE(nameE, graph, ...) \
{ \
auto GRAPHITERATOR(edges(graph)); \
auto& …
Run Code Online (Sandbox Code Playgroud) 我有Java 8 if语句的问题.如果只使用Java 8 lambdas,有人可以告诉我一种编写此代码的方法吗?解决方案不应该是if,while或者是.它甚至可能吗?
if (first_number == second_number) {
return "PERFECT";
} else if (first_number > second_number) {
return "ABUNDANT";
} else {
return "DEFICIENT";
}
Run Code Online (Sandbox Code Playgroud) 我可以描述这个“圆形”六边形网格的尺寸..
..n
在编译时只定义了1 个值:
const GRID_RADIUS: usize = 3;
Run Code Online (Sandbox Code Playgroud)
因此,网格中的单元格数量在编译时也是已知的,因为它是(2n+1)^2-n*(n+1)
(此处为 37)。
但是,以下内容:
const GRID_RADIUS: usize = 3;
Run Code Online (Sandbox Code Playgroud)
不编译:
const N: usize = 3;
const N_CELLS: usize = ((2 * N + 1) ^ 2) - N * (N + 1);
struct Cell;
struct Grid {
cells: [Cell; N_CELLS],
}
Run Code Online (Sandbox Code Playgroud)
我知道rustc
担心减去usize
类型可能会导致溢出,但我可以保证N_CELLS
在这种情况下总是积极的。
我怎么能为此承担责任并rustc
信任我?
compiler-errors constants integer-overflow unsigned-integer rust
c++ ×5
python ×3
inheritance ×2
numpy ×2
optimization ×2
pytorch ×2
arrays ×1
boost ×1
c++11 ×1
casting ×1
constants ×1
function ×1
graph ×1
insertion ×1
instance ×1
java ×1
lambda ×1
loops ×1
matrix ×1
member ×1
private ×1
protected ×1
python-3.x ×1
restriction ×1
rust ×1
standards ×1
static ×1
string ×1
templates ×1
torch ×1
unicode ×1
utf-8 ×1
vector ×1
vim ×1
while-loop ×1