小编jml*_*pez的帖子

Mathematica:标签和绝对定位

如何在mathematica中将文本放置在绘图之外?快速谷歌搜索将引导您

http://reference.wolfram.com/mathematica/howto/AddTextOutsideThePlotArea.html

这是不够的,因为你想用代码实现这一点.在mathematica中放置文本的一个简单示例如下:

    Show[ 
     Plot[x^3, {x, -1, 1},
      Frame -> True, 
      ImageSize -> Medium, 
      FrameLabel -> {"x", "y"},
      PlotRange -> {{-1, 1}, {-1, 1}}
      ],
     Graphics[
      Text[Style["A", Bold, 14, Red], {.5, .5}]]
     ]
Run Code Online (Sandbox Code Playgroud)

这将字母A放置在相对于图的点(.5,.5)处.有没有办法相对于图像的大小放置文本?据我所知,一切都在情节坐标中完成.治标不治本我是设置选项PlotRangeClippingFalse并给予正确的坐标设置文本.

Show[
    Plot[
        x^3, {x, -1, 1}, 
        Frame -> True, 
        ImageSize -> Medium, 
        FrameLabel -> {"x", "y"}, 
        PlotRange -> {{-1, 1}, {-1, 1}}
    ],
    Graphics[
        Text[
            Style["A", Bold, 14, Red], 
            {-1.2, 1}
        ]
    ],
    PlotRangeClipping -> False
]
Run Code Online (Sandbox Code Playgroud)

currentsolution

这种方法的一个缺点是,如果我们改变绘图的范围,那么我们需要重新计算文本的坐标,以便将其保持在我们想要的位置(相对于整个图像).

编辑:

尝试将TextA放在情节之外. …

wolfram-mathematica

5
推荐指数
1
解决办法
1万
查看次数

使用browserify/rollup/webpack为较小的构建加载单个lodash方法

来自https://lodash.com/:

// Load a single method for smaller builds with browserify/rollup/webpack.
var chunk = require('lodash/chunk');
var extend = require('lodash/fp/extend');
Run Code Online (Sandbox Code Playgroud)

:这与大多数的方法效果很好each,map,isArray,等一个方法我得粗野,工作是lodash/chain.

我导入整个lodash库的当前代码如下所示:

_.chain(items)
  .filter(...)
  .groupBy(...)
  .map(...)
  .concat(...)
  .value();
Run Code Online (Sandbox Code Playgroud)

创建一个不包含lodash构建中包含的所有方法的正确可链接对象的正确方法是什么?该chain方法创建一个lodash包装器对象并返回它.我可以像这样创建自己的链式方法

var lodash = require('lodash/wrapperLodash');
var filter = require('lodash/filter');
var map = require('lodash/map');

function chain(value) {
  var result = lodash(value);
  result.__chain__ = true;
  result.filter = filter
  result.map = map;
  return result;
}

module.exports = chain;
Run Code Online (Sandbox Code Playgroud)

现在调用chain将能够执行filtermap …

rollup browserify lodash webpack

5
推荐指数
1
解决办法
1173
查看次数

argparse 可选位置参数和子解析器参数

我有一个 python 脚本,它接受一个可选的位置参数并有一些子命令。其中一些子命令需要位置参数,有些则不需要。当我尝试使用不需要位置参数的子命令时,就会出现问题。考虑以下测试文件:

import argparse

argp = argparse.ArgumentParser()
argp.add_argument('inputfile', type=str, nargs='?',
                  help='input file to process')
argp.add_argument('--main_opt1', type=str,
                  help='global option')

subp = argp.add_subparsers(title='subcommands',
                           dest='parser_name',
                           help='additional help',
                           metavar="<command>")

tmpp = subp.add_parser('command1', help='command1 help')
tmpp.add_argument('pos_arg1', type=str,
                  help='positional argument')

print repr(argp.parse_args())
Run Code Online (Sandbox Code Playgroud)

当我尝试将子命令command1与第一个参数一起使用时,一切顺利。

macbook-pro:~ jmlopez$ python pytest.py filename command1 otherarg
Namespace(inputfile='filename', main_opt1=None, parser_name='command1', pos_arg1='otherarg')
Run Code Online (Sandbox Code Playgroud)

但现在让我们假设command1不需要第一个位置参数。

macbook-pro:~ jmlopez$ python pytest.py command1 otherarg
usage: pytest.py [-h] [--main_opt1 MAIN_OPT1] [inputfile] <command> ...
pytest.py: error: argument <command>: invalid choice: 'otherarg' (choose from 'command1')
Run Code Online (Sandbox Code Playgroud)

我不知何故期待inputfile …

python argparse

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

Mathematica:更改Graphics对象的选项

是否可以更改Graphics对象的选项?假设您正在使用图形对象G2D,如下图所示

样品

您可以从看到InputFormG2D,该PlotRange选项设置为{{-0.025,1.025},{0,1.05}}.但后来代码我决定将PlotRange选项改为另一个.怎么了InputForm?新选项只是附加.

您可以使用Options和获取图形对象设置的选项,AbsoluteOptions但我还没有找到更改这些选项的方法.该函数SetOptions似乎是一个可能的候选者,但事实证明,此函数仅适用于流和函数.也就是说,它只允许设置示例中显示的默认行为.

wolfram-mathematica

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

Mathematica:删除图形基元

鉴于这g是一个具有Lines和Polygons 等基元的图形对象,你如何删除其中的一些呢?要向现有图形对象添加更多基元,我们可以使用Show,例如:Show[g, g2]where g2是另一个具有其他基元的图形对象.但是如何删除不需要的原始对象?看一下以下内容

ListPlot3D[{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}}, Mesh -> {1, 1}]
Run Code Online (Sandbox Code Playgroud)

产量

现在,对于输入表单:

InputForm[
   ListPlot3D[{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}}, Mesh -> {1, 1}]
 ]
Run Code Online (Sandbox Code Playgroud)

产量

要从此对象创建线框,我们所要做的就是删除多边形.另外,我们还可以删除顶点法线,因为它们不会对线框有贡献.

请注意,要制作线框,我们只需将其设置PlotStyle -> None为选项即可ListPlot3D.这摆脱了Polygons,但没有删除VertexNormals.

澄清这个问题.鉴于

 g = ListPlot3D[{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}}, Mesh -> {1, …
Run Code Online (Sandbox Code Playgroud)

wolfram-mathematica

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

使用具有可变数量参数的宏编写函数

你如何编写一个具有可变数量的参数的宏来定义一个函数?假设我们class1用2个参数定义类,class2用3个参数定义类.

class class1 {
public:
   int arg1;
   int arg2;
   class1(int x1, int x2): arg1(x1), arg2(x2) {}
};
class class2 {
public:
   int arg1;
   int arg2;
   int arg3;
   class1(int x1, int x2, int x3): arg1(x1), arg2(x2), arg3(x3) {}
};
Run Code Online (Sandbox Code Playgroud)

对于我定义的每个类,甚至是在我想要编写以下内容之前定义的类:

template<> inline void writeInfo<class1>(const class1& obj, FILE* fp) {
    writeAmount(2, fp);
    writeName("arg1", fp);
    writeInfo(obj.arg1, fp);
    writeName("arg2", fp);
    writeInfo(obj.arg2, fp);
}
template<> inline void writeInfo<class2>(const class2& obj, FILE* fp) {
    writeAmount(3, fp);
    writeName("arg1", fp);
    writeInfo(obj.arg1, fp);
    writeName("arg2", fp); …
Run Code Online (Sandbox Code Playgroud)

c++

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

使用Manipulate和变量依赖的Multipanels

是否可以更改变量的值Manipulate?假设我有一Manipulate两个变量,xy我们显示的值.我想要做的是以这样的方式使它当我改变值时x,y更新为x*x.当我更改值时y,则x更新为平方根y.

另一个问题是,我可以有多个面板Manipulate吗?我想在每个滑块下面都有一个白色面板.

Manipulate[
 Row[{x, y}, "    "],
 Row[{
   Control[{{x, 0, Style["x", "TI", 14]}, 0, 4 , Appearance -> "Labeled"}],
   Control[{{y, 0, Style["y", "TI", 14]}, 0, 16, Appearance -> "Labeled"}]
 }]
] 
Run Code Online (Sandbox Code Playgroud)

例

在上面的情节中,我设置x为3和y9.再次,我想移动y说4并且必须x移动到2.同样,我想移到x4并y移动到16.可能吗?或者我刚遇到鸡肉或鸡蛋问题?

wolfram-mathematica

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

如何让C++知道运算符已经为类重载了

是否可以编写特定类型的类具有某些功能或重载运算符的模板函数?例如

template <typename T>
void dosomething(const T& x){
    std::cout << x[0] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

在这个上下文中,我假设x是一个行为类似于数组的类,也就是说,我已经重载了[]运算符以及<<它可以使用它std::cout.

我的实际代码略有不同,但gcc给了我

error: subscripted value is neither array nor pointer
Run Code Online (Sandbox Code Playgroud)

这一定是因为它不知道我期望T某些类重载[]运算符.有谁知道是否有可能克服这个问题?我想让c ++知道特定类型T会有[]重载.

c++

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

C宏保护定义

有没有办法保护宏定义?更具体地考虑以下内容:

#define macro1 x

// code segment 1 involving macro1 goes here 

// code segment 2 involving macro1 goes here

// code segment 3 involving macro1 goes here
Run Code Online (Sandbox Code Playgroud)

在示例中,我放置了3条注释,表示涉及宏的代码段.我现在想做的是能够避免宏影响代码段2.有没有办法告诉编译器替换段1和段3中的所有macro1实例而不是段2中的宏实例?这是一种可能的方式:

#define macro1 x

// code segment 1 involving macro1 goes here 

#undef macro1

// code segment 2 involving macro1 goes here

#define macro1 x

// code segment 3 involving macro1 goes here
Run Code Online (Sandbox Code Playgroud)

缺点是我必须再次定义宏.说我想在我的程序中使用NULL这个词(不要问我为什么要用它).我希望这是一个变量,但在大多数情况下,C预处理器会将其更改为0.所以我想做的是,能够在短时间内阻止它,然后让它成为以前的样子.

尝试失败:

让我们假设macro1已在外部定义,我们甚至不知道这个宏的值是什么.我们想要的只是避免让它替换第二段中的东西.

// code segment 1 involving macro1 goes here 

#ifdef macro1
#define TEMP macro1
#undef macro1
#endif …
Run Code Online (Sandbox Code Playgroud)

c c++ c-preprocessor

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

模板函数名称的模板参数

我一直在寻找与我的问题有关的例子,但我仍然无法找到解决方案.我发现的最接近的是

模板函数作为模板参数

我将尝试发布一个工作示例,以防需要它,但到目前为止我的部分代码涉及以下内容:

template<class InterfaceType, class T> 
inline void write_info(InterfaceType& interface, T& t) {
    InterfaceType::write_info(interface, t);
}

template<class InterfaceType, class T> 
inline void write_data(InterfaceType& interface, T& t) {
    InterfaceType::write_data(interface, t);
}

template<class InterfaceType, class T> 
inline void write_definition(InterfaceType& interface, T& t) {
    InterfaceType::write_definition(interface, t);
}
Run Code Online (Sandbox Code Playgroud)

请注意,模板write_info依赖于具有名为write_info(静态方法)的方法的接口类型.这样做的原因是因为write_info函数可以在以后针对特定数据类型进行专门化,而无需重新定义任何内容InterfaceType.

简单的问题是:我们可以使用将函数命名为函数参数的模板来减少上面的代码吗?请记住,我真的希望这是可能的,这样我就可以避免为专门的数据类型定义所有这3个函数,即

假设foo是两个属性的结构int adouble b.然后我可以像这样专门化上面的函数:

template<class InterfaceType> 
inline void write_info(InterfaceType& interface, foo& t) {
    InterfaceType::write_info(interface, t.a);
    InterfaceType::write_info(interface, t.b);
}

template<class InterfaceType> …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading

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

python中更快的属性访问

考虑以下课程:

class MyObject(object):

    __slots__ = ('_att1', '_att2')

    def __init__(self):
        self._att1 = None
        self._att2 = None

    @property
    def att1(self):
        """READ-ONLY property. """
        return self._att1

    @property
    def att2(self):
        """att2 property description. """
        return self._att2

    @att2.setter
    def att2(self, val):
        self._att2 = val
Run Code Online (Sandbox Code Playgroud)

使用属性装饰器的一个优点是我们可以添加一些文档

a = MyObject()
help(a)
Help on MyObject in module __main__ object:

class MyObject(__builtin__.object)
 |  Methods defined here:
 |  
 |  __init__(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  att1
 |      READ-ONLY property.
 |  
 |  att2
 |      att2 property …
Run Code Online (Sandbox Code Playgroud)

python pylint

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