我需要规范化N个整数的向量,以便:
例如:
如果我有一个矢量
V = [2,2,1,0]
Run Code Online (Sandbox Code Playgroud)
规范化的矢量应该是:
V_norm = [0.4,0.4,0.2,0] % 0.4+0.4+0.2 = 1
Run Code Online (Sandbox Code Playgroud)
我尝试了在这个社区和网络上找到的许多解决方案,最后我用这个代码做了:
part = norm(V);
if part > 0
V_norm = V/part;
else % part = 0 --> avoid "divide by 0"
V_norm = part;
end
Run Code Online (Sandbox Code Playgroud)
这有效的问题如果:
但如果我有不同的情况,虽然结果是成比例的,但总和不是0.例如:
V = [1,0,1]
V_norm = [0.74,0,0.74]
V = [1,1,1]
V_norm = [0.54,0.54,0.54]
Run Code Online (Sandbox Code Playgroud)
(我不确定这个数字是否正确,因为我现在不能使用Matlab,但我确定总和> 1)
Ahy暗示?
先感谢您
在我的Angular 2应用程序(SystemJS模块管理器,作为脚本语言的Typescript)中,我需要导入一个npm模块来处理加密(Crypto-JS; Forge-JS或任何其他服务目的)
在CryptoJS的情况下,通过npm install*安装后我尝试添加:
<script src="node_modules/crypto-js/crypto-js.js"></script>
Run Code Online (Sandbox Code Playgroud)
in index:html.
在我的服务(app/services/my-service.service.ts)我通过它导入它
import {CryptoJS} from 'node_modules/crypto-js/crypto-js.js' // or /aes.js --> same issue
Run Code Online (Sandbox Code Playgroud)
但是,导入无法正常工作
console.log(CryptoJS);
Run Code Online (Sandbox Code Playgroud)
打印未定义.
我还尝试添加模块路径
System.config({
// ...
map: {
CryptoJS
}
}
Run Code Online (Sandbox Code Playgroud)
并通过我的服务导入它
import {CryptoJS} from 'cryptoJs';
Run Code Online (Sandbox Code Playgroud)
虽然我不确定我应该在SystemJS配置中实际放入什么,但我尝试过的解决方案都没有.
编辑我也试过......
// import ... as to overcome no default export
import * as CryptoJS from 'node_modules/crypto-js/crypto-js.js';
Run Code Online (Sandbox Code Playgroud)
但是之后
console.log(CryptoJS.);
Run Code Online (Sandbox Code Playgroud)
不提供AES /任何方法(我的编辑通常建议我可以通过自动完成使用哪些方法)
编辑2现在感谢Thierry和PierreDuc的贡献很明显,打字和模块导入是无关联的概念.
然而,他们都没有工作.这就是我所做的:
我下载了CryptoJS打字文件,把它放在typings/cryptojs/cryptojs.d.ts中
然后我补充说
/// …Run Code Online (Sandbox Code Playgroud) api/[...nextauth].js
export default NextAuth({
providers: [
Providers.Credentials({
name: 'Credentials',
credentials: {
email: { label: 'E-mail', type: 'text', placeholder: 'me@gmail.com' },
password: { label: 'Password', type: 'password', placeholder: 'my password' },
},
async authorize(credentials, req) {
return { email: 'x', password: 'y'}
},
}),
]
});
Run Code Online (Sandbox Code Playgroud)
页面/auth/signin.tsx
export default function SignIn() {
const [password, setPassword] = useState(null)
const [email, setEmail] = useState(null)
const onSubmit = () => {
signIn("Credentials", { email, password })
}
return (
<div>
<label>
Email
<input name="email" …Run Code Online (Sandbox Code Playgroud) 在我的Angular2-typescript应用程序中,我只使用forkJoin在所有并行HTTP调用完成后返回一个Observable.
问题:订阅回调一直无限期地执行
这是我的代码:
http.service
import {Http} from "@angular/http";
constructor (private _http: HTTP) {}
makeGetRequest(....) {
return this._http.get(URL)
.map (res => res.json)
.toPromise();
Run Code Online (Sandbox Code Playgroud)
my.service
import {Observable} from "rxjs/Observable";
import {HttpService} from "http.service"
constructor (private _httpService: HttpService) {}
myMethod(): Observable<any[]> {
return Observable.forkJoin(
this._httpService.makeGetRequest(
URL1
),
this._httpService.makeGetRequest(
URL2
)
)
}
Run Code Online (Sandbox Code Playgroud)
my.component
import MyService from "my.service";
import Subscription from "rxjs";
constructor (private _service: MyService) {}
mySub: Subscription;
ngOnInit() {
this.mySub = this._service.myMethod.subscribe(data => {
data.forEach(console.log(data));
this.mySub.unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过(同样的问题):
在我的eslint配置(YAML格式)中,我扩展了 3 种不同的配置:
extends:
- airbnb-base
- plugin:angular/johnpapa
- ionic
Run Code Online (Sandbox Code Playgroud)
我的问题如下:
YAML吗?eslint:recommended):如果错误与这些“共享”规则之一有关,我会多次收到相同的错误吗?我已经通过Ionic 3文档,我试图了解它们之间的区别
https://ionicframework.com/docs/api/components/virtual-scroll/VirtualScroll/
和
https://ionicframework.com/docs/api/components/infinite-scroll/InfiniteScroll/
我看到他们使用不同的组件,虽然他们展示了InfiniteScroll的一个例子,但没有VirtualScroll的例子,它的设置也看起来比较棘手.
两者之间有什么区别,什么是可能的用例何时使用其中一个?
我在我的Angular(v1.x)应用程序中使用angular-translate以及使用$ translateProvider.useStaticFilesLoader从文件加载翻译
angular.module('myApp',['pascalprecht.translate'])
.config(function($translateProvider) {
$translateProvider.useStaticFilesLoader(
prefix: window.location.pathname.replace(/[\\\/][^\\\/]*$/, '') + '/__languages/', // absolute path + language path
suffix: '.json'
});
// $translateProvider.useLocalStorage();
}
Run Code Online (Sandbox Code Playgroud)
Angular-translate工作得很好,但在使用时:
$translateProvider.useLocalStorage();
Run Code Online (Sandbox Code Playgroud)
(在上面的代码中评论)如本指南中提出的,我收到以下错误:
未捕获错误:[$ injector:unpr]未知提供者:$ translateLocalStorageProvider < - $ translateLocalStorage < - $ translate
当然,我包含了所有需要的js文件:
<script src="bower_components/angular-translate/angular-translate.min.js"></script>
<script src="bower_components/angular-translate-loader-url/angular-translate-loader-url.min.js"></script>
<script src="bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
我甚至试图:
我正在研究遗传算法.
有两个目标,每个目标都有自己的适应值(fv1,fv2).
我知道世代(SGE)和稳态(SS)遗传算法是如何工作的.
我试图了解NSGA-2和SPEA-2(我正在使用Java库JCLEC的实现)如何工作,特别是:
如果有人正在使用JCLEC库,这些是我设置的参数:
我正在以下网址关注React教程:http://facebook.github.io/react/docs/tutorial.html
我只是在 http://facebook.github.io/react/docs/tutorial.html#fetching-from-the-server 之后
我在SO上经历了类似的问题,但没有找到针对我的特定情况的解决方案。
var data = [
{author: "Pete Hunt", text: "This is one comment"},
{author: "Jordan Walke", text: "This is *another* comment"},
{author: "Bob Lilly", text: "This is *another* comment 2"}
];
var Comment = React.createClass({
render: function() {
var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
return (
<div className="comment">
<h2 className="commentAuthor">
{this.props.author}
</h2>
<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
</div>
);
}
});
var CommentList = React.createClass({
render: function() {
var commentNodes = …Run Code Online (Sandbox Code Playgroud) 在我的Angular 2应用程序中,我有一个带有输入字段的组件,它应该接受一系列数字.
更具体地说,2例:
我在用
<form>
<input type="text" pattern="[0-9]"> <!-- 0-9 -->
<input type="text" pattern="\d|1\d|2[0-3]"> <!-- 0-23 -->
<input type="text" pattern="\d\d"> <!-- [0-99] -->
</form>
Run Code Online (Sandbox Code Playgroud)
问题是我基本上可以输入任何内容(就像忽略验证一样),包括文本.我不认为这是与Angular 2相关的问题,因为标准验证有效,例如
<input type="number">
Run Code Online (Sandbox Code Playgroud)
允许只输入数字(但任何数字不是我想要的)
我还尝试使用类型编号的min = 0和max = 23(或59)属性,但这也不起作用.