小编ena*_*one的帖子

否定std :: vector的最快方法

假设我有一个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更快的函数,我很高兴知道.

c++ optimization blas stdvector lapack

26
推荐指数
2
解决办法
3335
查看次数

BLAS矩阵逐矩阵转置乘法

我必须以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?谢谢!

matrix linear-algebra blas

4
推荐指数
1
解决办法
1273
查看次数

TypeScript claims no error even if parameter has the wrong type

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)

javascript return-type typescript tsc

4
推荐指数
1
解决办法
2141
查看次数

在 C++ 类中有条件地启用构造函数

我正在学习如何使用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)

c++ constructor enable-if

3
推荐指数
1
解决办法
2270
查看次数

在鱼壳中管道 eval/source 的输出

我想将 的输出通过管道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)

eval file-descriptor io-redirection fish

2
推荐指数
1
解决办法
437
查看次数

创建异步迭代器的最佳实践是什么?我应该使用异步生成器函数还是使用 Symbol.asyncIterator?

此代码按预期工作:

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)

javascript iterator generator node.js async-await

2
推荐指数
1
解决办法
2359
查看次数