假设我有一个double的std :: vector,即
std::vector<double> MyVec(N);
Run Code Online (Sandbox Code Playgroud)
在哪里N
这么大,性能很重要.现在假设这MyVec
是一个非平凡的向量(即它不是一个零向量,但已被某些例程修改).现在,我需要向量的否定版本:我需要-MyVec
.
到目前为止,我一直在实施它
std::transform(MyVec.cbegin(),MyVec.cend(),MyVec.begin(),std::negate<double>());
Run Code Online (Sandbox Code Playgroud)
但是,实际上,我不知道这是否合情合理,或者只是我身边的超级天真.
我做得对吗?或者std :: transform在这种情况下只是一个超级慢的例程?
PS:我一直在使用BLAS和LAPACK库,但是我没有发现任何符合这种特殊需求的东西.但是,如果BLAS/LAPACK中存在比std :: transform更快的函数,我很高兴知道.
我必须以A'A
或更一般的形式计算一些乘积A'DA
,其中A
是一般mxn
矩阵,D
是对角mxm
矩阵。他们两个都是全职。即rank(A)=min(m,n)
。
我知道这样的对称乘积可以节省大量时间:鉴于对称的乘积A'A
,您只需要计算乘积矩阵的对角线的下部或上部。这增加了n(n+1)/2
要计算的条目,大约是n^2
大型矩阵典型值的一半。
我想利用这节省了很多钱,而且我知道我可以在for
循环中实现矩阵-矩阵乘法。但是,到目前为止,我一直在使用BLAS,它比for
我自己可以编写的任何循环实现都要快得多,因为它可以优化缓存和内存管理。
有没有一种方法可以有效地计算A'A
甚至A'DA
使用BLAS?谢谢!
I have a function that takes a dictionary as the first argument. This dictionary has string as keys, and functions as values. The problem is that, if a function in the dictionary has a bad signature, TypeScript does not complain!
A piece of code is worth 1000 words. Here's my main.ts
:
interface MyMap {
[key: string]: (x: number) => void;
}
function f(map: MyMap, key: string, x: number) : void{
map.hasOwnProperty(key) ? map[key](x) : console.log(x)
}
const stupidMap: MyMap …
Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用std::enable_if
,到目前为止,我在有条件地启用和禁用课程中的方法方面取得了一定程度的成功。我针对布尔值对方法进行模板化,并且此类方法的返回类型是std::enable_if
此类布尔值。这里的最小工作示例:
#include <array>
#include <iostream>
#include <type_traits>
struct input {};
struct output {};
template <class io> struct is_input { static constexpr bool value = false; };
template <> struct is_input<input> { static constexpr bool value = true; };
template <class float_t, class io, size_t n> class Base {
private:
std::array<float_t, n> x_{};
public:
Base() = default;
Base(std::array<float_t, n> x) : x_(std::move(x)) {}
template <class... T> Base(T... list) : x_{static_cast<float_t>(list)...} {}
// Disable the getter if it …
Run Code Online (Sandbox Code Playgroud) 我想将 的输出通过管道eval
传输到文件。如果命令执行成功,这将按预期工作:
eval ls > log.txt 2>&1
cat log.txt # Documents Desktop
Run Code Online (Sandbox Code Playgroud)
如果命令不成功,它也有效
eval rm Desktop > log.txt 2>&1
cat log.txt # rm: cannot remove 'Desktop': Is a directory
Run Code Online (Sandbox Code Playgroud)
但是,如果命令不存在,我无法重定向 stderr
eval abcde > log.txt 2>&1 # fish: Unknown command abcde
cat log.txt # (empty)
Run Code Online (Sandbox Code Playgroud)
如何将第三种情况的输出也重定向到日志文件?
一些与工作source
也将非常赞赏:
echo abcde | source > log.txt 2>&1
Run Code Online (Sandbox Code Playgroud) 此代码按预期工作:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getAsyncData() {
await sleep(1000); // simulate database/network delay...
return [1, 2, 3, 4, 5]; // ...then return some data
}
const asyncIterable = (async function* filterAsyncData() {
const items = await getAsyncData();
for (const item of items) {
yield item;
}
})();
const asyncIterable2 = {
[Symbol.asyncIterator]() {
return {
values: null,
idx: 0,
async next() {
if (this.values === null) {
this.values = await getAsyncData();
} …
Run Code Online (Sandbox Code Playgroud) blas ×2
c++ ×2
javascript ×2
async-await ×1
constructor ×1
enable-if ×1
eval ×1
fish ×1
generator ×1
iterator ×1
lapack ×1
matrix ×1
node.js ×1
optimization ×1
return-type ×1
stdvector ×1
tsc ×1
typescript ×1