在C++参考页面中,他们提供了一些typedef示例,我试图理解它们的含义.
// simple typedef
typedef unsigned long mylong;
// more complicated typedef
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];
Run Code Online (Sandbox Code Playgroud)
所以我理解的是简单的typedef(第一个声明).
但是他们用第二个宣告什么(下面重复)?
typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
Run Code Online (Sandbox Code Playgroud)
特别是什么(&fp)(int, mylong)意思?
我创建了以下功能:
nDone <- function(under,strike,ttoe,vol,rf,dy) {
pnorm(((log(under/strike)+ (rf-dy+(vol^2)/2)*ttoe)/(vol*(ttoe^0.5))))
}
nDone(90,100,3,0.17,0.05,0)
# Result:
[1] 0.6174643
Run Code Online (Sandbox Code Playgroud)
然后我用以下函数调用该函数:
d <- c(90,100,3,0.17,0.05,0)
nDone(d)
Error in under/strike : 'strike' is missing
Run Code Online (Sandbox Code Playgroud)
结果
nDone <- function(under,strike,ttoe,vol,rf,dy) {
pnorm(((log(under/strike)+ (rf-dy+(vol^2)/2)*ttoe)/(vol*(ttoe^0.5))))
}
nDone(90,100,3,0.17,0.05,0)
# Result:
[1] 0.6174643
Run Code Online (Sandbox Code Playgroud)
好到目前为止都很好.
现在我在对象中创建一个具有相同值的向量:
d <- c(90,100,3,0.17,0.05,0)
nDone(d)
Error in under/strike : 'strike' is missing
Run Code Online (Sandbox Code Playgroud)
我尝试使用该对象调用该函数.
nDone <- function(under,strike,ttoe,vol,rf,dy) {
pnorm(((log(under/strike)+ (rf-dy+(vol^2)/2)*ttoe)/(vol*(ttoe^0.5))))
}
nDone(90,100,3,0.17,0.05,0)
# Result:
[1] 0.6174643
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
d <- c(90,100,3,0.17,0.05,0)
nDone(d)
Error in under/strike : 'strike' is missing
Run Code Online (Sandbox Code Playgroud)
我做错了什么以及如何解决?
谢谢
RSG
我一直在尝试通过尝试不同的变量和函数以及查看结果来了解plyr的工作原理和方式.因此,我更多地寻找有关plyr如何工作的解释,而不是特定的解决方案.我已经阅读了文档但我的新手大脑仍然没有得到它.
一些数据和名称:
mydf<- data.frame(c("a","a","b","b","c","c"),c("e","e","e","e","e","e")
,c(1,2,3,10,20,30),
c(5,10,20,20,15,10))
colnames(mydf)<-c("Model", "Class","Length", "Speed")
mydf
Run Code Online (Sandbox Code Playgroud)
问题1:汇总与转换语法
所以,如果我输入: ddply(mydf, .(Model), summarise, sum = Length+Length)
我明白了:
`Model ..1
1 a 2
2 a 4
3 b 6
4 b 20
5 c 40
6 c 60
Run Code Online (Sandbox Code Playgroud)
如果我输入:ddply(mydf, .(Model), summarise, Length+Length)我得到相同的结果.
现在如果使用转换: ddply(mydf, .(Model), transform, sum = (Length+Length))
我明白了:
Model Class Length Speed sum
1 a e 1 5 2
2 a e 2 10 4
3 b e 3 20 6
4 b e 10 …Run Code Online (Sandbox Code Playgroud) 我一直爬着陡峭的WPF山!所以我想创建一个允许用户动态添加文本框的UI.要做到这一点,他们会点击一个按钮.
我已经设法使用后面的代码创建它,但我想转向MVVM结构,所以我在视图中没有任何代码.我已经尝试过ICommand和ObservableCollection,但我遗漏了一些东西而且我不知道在哪里.这是我的简单例子.
XAML:非常基本,只有一个按钮可以添加一行.
<Window x:Class="WPFpractice072514.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFpractice072514"
Title="MainWindow" Height="350" Width="525">
<Grid Name="mymy" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Grid.Row="0" Name="ButtonUpdateArtist"
Content="Add TextBox" Click="ButtonAddTexboxBlockExecute" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
C#代码背后
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFpractice072514
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// …Run Code Online (Sandbox Code Playgroud) 假设我有一个列表:
alist<- list(4,6,8,9)
Run Code Online (Sandbox Code Playgroud)
我希望测试每个列表元素是否大于7,如果为真,则返回1的列表,如果为false则返回0.
但是我必须使用lapply.
lapply(alist,if,>7,1) or lapply(alist,if,cond>7,1)
Run Code Online (Sandbox Code Playgroud)
当然这些都不起作用,我不断收到以下错误.
Error: unexpected ',' in "lapply(alist, if,"
Run Code Online (Sandbox Code Playgroud) 首先,这个问题不是试图解决具体问题.作为R的新手,我也在努力创建更高效的代码和代码构建过程.获得关于不同编程方法甚至样式的观点是这个问题背后的原因.
以下是三种编码方式:
首先是示例数据:
stackexample <- c(52,50,45,49.5,50.5,12,10,14,11.5,12,110,108,106,101,104)
dim(stackexample)<- c(5,3)
Run Code Online (Sandbox Code Playgroud)
方法一:在函数中进行数学运算而不定义任何对象
ertimesIVCV1 <- function (x)
{ (solve(var(log((x[-nrow(x),])/(x[-1,])))))%*%
((1+(log(x[1,]/(x)[nrow(x),])))^(1/nrow(x))-1)}
ertimesIVCV1(stackexample)
Run Code Online (Sandbox Code Playgroud)
方法二:在函数中定义对象,然后操纵这些对象
ertimesIVCV2 <- function (x)
{ IVCV <- solve(var(log((x[-nrow(x),])/(x[-1,]))));
retsexcess <- (1+(log(x[1,]/(x)[nrow(x),])))^(1/nrow(x))-1;
IVCV%*%retsexcess}
ertimesIVCV2(stackexample)
Run Code Online (Sandbox Code Playgroud)
方法三:定义几个函数并在"类似摘要"函数中调用这些函数
IVCV <- function (x) {solve(var(log((x[-nrow(x),])/(x[-1,]))))}
retsexcess <- function(x) (1+(log(x[1,]/(x)[nrow(x),])))^(1/nrow(x))-1
ertimesIVCV3 <- function (x) {IVCV(x)%*%retsexcess(x)}
ertimesIVCV3(stackexample)
Run Code Online (Sandbox Code Playgroud)
所以都产生了相同的答案:
[,1]
[1,] 1.4430104
[2,] -0.1365155
[3,] 11.8088378
Run Code Online (Sandbox Code Playgroud)
但正如你可以看到三种不同的方法.
是否存在嵌入式函数的最佳数量,还是应该总是尝试明确列出所有数学?函数中有多少级别的函数是最佳的?这两种方法的计算速度都优越吗?对此有经验吗?你怎么看待这个?欢迎任何意见或建议或链接,谢谢!
黑麦
该问题的示例。
档案结构
foo_folder
|
bar_module.py
| |___foo_func
| |____init___
python application folder
|___python_solution.sln, .proj ...
|___python_application.py
Run Code Online (Sandbox Code Playgroud)
bar_module.py:
def foo_func():
return 3.14
Run Code Online (Sandbox Code Playgroud)
python_application.py
from clients.reddit import foo_module
if __name__ == '__main__':
print(foo_module.foo_func())
Run Code Online (Sandbox Code Playgroud)
我有一个项目和一个.py文件的解决方案。该项目中的文件将模块导入到项目外部。现在在PowerShell中可以正常工作。但是在Visual Studio中却没有。这些是问题:
出现导入错误时,无法从Visual Studio运行此代码。
Intellisense不起作用。当我在python_application.py中工作时,没有intellisensefor bar_module。
现在我意识到我可以添加bar_module.py到项目中,但这会生成我不想要的本地副本。我需要更改什么以便可以从Visual Studio运行此代码并使Intellisense工作?
对于初学者练习,我试图创建一个简单的循环,它接受来自用户的单个字符,将该字符打印到控制台并继续这样做,直到用户输入“R”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleLoop
{
class Program
{
static void Main(string[] args)
{
char cplayerSelection = 'R';
while(cplayerSelection == 'R')
{
Console.WriteLine("Enter R, P, or S:");
cplayerSelection = (char)Console.Read();
Console.WriteLine(cplayerSelection);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
无论用户输入什么,它只会循环一次结束然后退出。我需要更改什么才能继续循环?
我一直在尝试简单的实验来学习C#方法.下面的代码只是调用playerSelection(),它向用户询问一个字符并将该字符返回给Main(string [] args).主要打印到控制台.使用下面的代码我得到以下错误"非静态字段需要对象引用."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace SimpleFunction
{
class Program
{
static void Main(string[] args)
{
char cplayerChoice = playerSelection();
Console.WriteLine(cplayerChoice);
}
char playerSelection()
{
Console.WriteLine("\nEnter a Character");
char cplayerChoice = Console.ReadKey().KeyChar;
return cplayerChoice;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在如果我像这样添加单词static:
static char playerSelection()
Run Code Online (Sandbox Code Playgroud)
它编译和工作.我确实理解静态与非......抽象.
我正在从一本书中学习C#,在那本书中,他们通过下面的例子来说明使用方法:
using System;
namespace GetinPaid
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
double dailyRate = readDouble("Enter your daily …Run Code Online (Sandbox Code Playgroud) 我正在尝试渲染必须通过的数据useMemo。我从 api 调用获取数据,因此我调用useEffect. 初始渲染工作返回一个空数组,但是一旦我调用 api,数据就不会更新。
import React from 'react'
import {fetchAccounts} from '../utils/api'
export default function Accounts() {
const [accounts, setAccounts] = React.useState([])
React.useEffect(() => {
setLoading(true)
fetchAccounts().then((data) => {
setAccounts(data)
})
}, [])
const data = React.useMemo(() => accounts, [])
return <p>{JSON.stringify(data)}</p>}
Run Code Online (Sandbox Code Playgroud)
那么这可以在单个组件中工作吗?或者必须创建自定义挂钩?
我创建了这个函数:
nDone<- function (under,strike,ttoe,vol,rf,dy) {
return(pnorm(((log(under/strike) + (rf-dy+(vol^2)/2)*ttoe)/(vol*(ttoe^0.5)))))
}
nDone(90,100,3,0.17,0.05,0)
[1] 0.6174643
Run Code Online (Sandbox Code Playgroud)
到目前为止,这很好并且有效.现在我想将函数应用于矩阵的每一行.
b<- c(90,95,100,100,3,2,0.17,0.18,0.05,0.05,0,0)
dim(b) <- c(2,6)
Run Code Online (Sandbox Code Playgroud)
这使:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 90 100 3 0.17 0.05 0
[2,] 95 100 2 0.18 0.05 0
Run Code Online (Sandbox Code Playgroud)
所以现在我想将每行中的元素传递给函数.我尝试过使用申请:
apply(b,1,nDone)
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
Error in under/strike : 'strike' is missing
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
lapply(b,nDone)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error in under/strike : 'strike' is missing
Run Code Online (Sandbox Code Playgroud)
我想要的是该功能的多个结果.我在这做错了什么?
我有以下最小化的功能:
calloptim <- function( under,strike, rf, ttoe,par) {(-(under*par[1]
-strike*exp(-rf*ttoe)*par[2]))^2}
Run Code Online (Sandbox Code Playgroud)
我创建了以下对象:
res<- nlminb(c(1,1), calloptim, under= 90, strike = 100, rf =0.05, ttoe=3)
res
$par
[1] 0.9771973 1.0218072
$objective
[1] 3.412923e-16
$convergence
[1] 1
$iterations
[1] 2
$evaluations
function gradient
34 4
$message
[1] "false convergence (8)"
Run Code Online (Sandbox Code Playgroud)
这很好,但现在我想抓住$ par估计值并将它们放在一个向量中,这样我就可以将它们用于其他计算.
如何隔离和保存对象的部分结果?
r ×6
c# ×3
function ×2
arguments ×1
c++ ×1
if-statement ×1
intellisense ×1
lapply ×1
methods ×1
mvvm ×1
plyr ×1
process ×1
python ×1
reactjs ×1
static ×1
typedef ×1
use-effect ×1
while-loop ×1
wpf ×1
xaml ×1