array

Numpy array dimensions

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm currently trying to learn Numpy and Python. Given the following array: import numpy as N a = N.array([[1,2],[1,2]]) Is there a function that returns the dimensions of a (e.g.a is a 2 by 2 array)? size() returns 4 and that doesn't help very much. 回答1: It is .shape : ndarray. shape Tuple of array dimensions. Thus: >>> a.shape (2, 2) 回答2: import numpy as np >>> np.shape(a) (2,2) Also works if the input is not a numpy array but a list of lists >>> a = [[1,2],[1,2]] >>> np.shape(a) (2,2) 回答3: First: By convention, in Python world, the

How do I find out eigenvectors corresponding to a particular eigenvalue of a matrix?

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do I find out eigenvectors corresponding to a particular eigenvalue? I have a stochastic matrix(P), one of the eigenvalues of which is 1. I need to find the eigenvector corresponding to the eigenvalue 1. The scipy function scipy.linalg.eig returns the array of eigenvalues and eigenvectors. D, V = scipy.linalg.eig(P) Here D(array of values) and V(array of vectors) are both vectors. One way is to do a search in D and extract the corresponding eigenvector in V. Is there an easier way? 回答1: If you are looking for one eigenvector

Pointer arithmetic and arrays: what's really legal?

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Consider the following statements: int * pFarr , * pVarr ; int farr [ 3 ] = { 11 , 22 , 33 }; int varr [ 3 ] = { 7 , 8 , 9 }; pFarr = &( farr [ 0 ]); pVarr = varr ; At this stage, both pointers are pointing at the start of each respective array address. For *pFarr, we are presently looking at 11 and for *pVarr, 7. Equally, if I request the contents of each array through *farr and *varr, i also get 11 and 7. So far so good. Now, let's try pFarr++ and pVarr++ . Great. We're now looking at 22 and 8, as expected. But now... Trying to

How can I find out which cache line is touched by an instruction on an Intel processor?

匿名 (未验证) 提交于 2019-12-03 03:02:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I read the article about the Meltdown/Spectre exploit that allow reading privileged data from the kernel using hardware bugs in the CPU. It says: The trick is to line up instructions in a normal user process that cause the processor to speculatively fetch data from protected kernel memory before performing any security checks. The crucial Meltdown-exploiting x86-64 code can be as simple as... ; rcx = kernel address ; rbx = probe array retry: mov al, byte [rcx] shl rax, 0xc jz retry mov rbx, qword [rbx + rax] An exception is raised, and

Scipy filter with multi-dimensional (or non-scalar) output

匿名 (未验证) 提交于 2019-12-03 02:59:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a filter similar to ndimage 's generic_filter that supports vector output? I did not manage to make scipy.ndimage.filters.generic_filter return more than a scalar. Uncomment the line in the code below to get the error: TypeError: only length-1 arrays can be converted to Python scalars . I'm looking for a generic filter that process 2D or 3D arrays and returns a vector at each point. Thus the output would have one added dimension. For the example below I'd expect something like this: m.shape # (10,10) res.shape # (10,10,2) Example

Javascript ArrayBuffer to Hex

匿名 (未验证) 提交于 2019-12-03 02:59:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got a Javascript ArrayBuffer that I would like to be converted into a hex string. Anyone knows of a function that I can call or a pre written function already out there? I have only been able to find arraybuffer to string functions, but I want the hexdump of the array buffer instead. 回答1: function buf2hex(buffer) { // buffer is an ArrayBuffer return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(''); } // EXAMPLE: const buffer = new Uint8Array([ 4, 8, 12, 16 ]).buffer; console.log(buf2hex

Fastest x86 assembly code to synchronize access to an array? [closed]

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What is the fastest x86 assembly code to synchronize access to an array in memory? To be more precise: We have a malloc'ed continuous single-paged region in memory and the OS will not page-out this region for the duration of our experiment. One thread will write to the array, one thread will read from the array. the array is small, but larger than the atomic-write capability of your cpu (so that a separate lock is acutally required) "fastest": the effective speed: Do not just assume the length of bytecode is significant but take into account

inpolygon for Python - Examples of matplotlib.path.Path contains_points() method?

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have been searching for a python alternative to MATLAB's inpolygon() and I have come across contains_points as a good option. However, the docs are a little bare with no indication of what type of data contains_points expects: contains_points(points, transform=None, radius=0.0) Returns a bool array which is True if the path contains the corresponding point. If transform is not None, the path will be transformed before performing the test. radius allows the path to be made slightly larger or smaller. I have the polygon stored as an n*2

How can I sort multiple arrays based on the sorted order of another array

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have multiple arrays, and I want to sort all of them based on the sorted order of one of them, like so: var myArr = ["b", "a", "c"] var myArr2 = ["letter b", "letter a", "letter c"] var myArr3 = ["b is the second letter", "a is the first letter", "c is the third letter"] func sortMultipleArraysBasedOnOne(alphabeticallyArray:Array, arrays:[Array]){ //order myArr alphabetically for array in arrays{ //change all arrays indexes like in myArr } } sortMultipleArraysBasedOnOne(myArr, [myArr2, myArr3]) I expect after function execution the arrays

Efficient way to find the max number in an array

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This is an interview question There is an array of integers. The elements in the array can follow the following patterns. numbers are in ascending order numbers are in descending order numbers increases in the beginning and decreases in the end numbers decreases in the beginning and increases in the end What is the efficient way to find the max number in the array? 回答1: In that case, all you need to do is to determine whether it's (3). If not, the answer is max(first, last). In the case that all elements are equal, you'll need to