operation

How do I perform a deletion of the kth element on a min-max heap?

邮差的信 提交于 2019-12-04 19:06:39
A min-max heap can be useful to implement a double-ended priority queue because of its constant time find-min and find-max operations. We can also retrieve the minimum and maximum elements in the min-max heap in O(log 2 n) time. Sometimes, though, we may also want to delete any node in the min-max heap, and this can be done in O(log 2 n) , according to the paper which introduced min-max heaps : ... The structure can also be generalized to support the operation Find(k) (determine the kth smallest value in the structure) in constant time and the operation Delete(k) (delete the kth smallest value

Dot Product in Python without NumPy

孤人 提交于 2019-12-04 07:20:08
Is there a way that you can preform a dot product of two lists that contain values without using NumPy or the Operation module in Python? So that the code is as simple as it could get? For example: V_1=[1,2,3] V_2=[4,5,6] Dot(V_1,V_2) Answer: 32 Without numpy, you can write yourself a function for the dot product which uses zip and sum . >>> def dot(v1, v2): ... return sum(x*y for x,y in zip(v1,v2)) ... >>> dot([1,2,3], [4,5,6]) 32 来源: https://stackoverflow.com/questions/35208160/dot-product-in-python-without-numpy

floating-point operations with bash

允我心安 提交于 2019-12-03 14:38:51
how can I transform the string "620/100" into "6.2" in a bash script The context of my question is about image processing. EXIF data are coding the focal length in fractional format, while I need the corresponding decimal string. Thanks for helping, Olivier Use bc -l bc -l <<< "scale=2; 620/100" 6.20 OR awk: awk 'BEGIN{printf "%.2f\n", (620/100)}' 6.20 bash doesn't support floating point. You could use bc : $ echo "50/10" | bc -l 5.00000000000000000000 $ echo "scale=1; 50/10" | bc -l 5.0 Thank-you for the answers. bc was what I needed. I dont know if posting the result is of any use. Anyway,

How to operate elementwise on a matrix of type scipy.sparse.csr_matrix?

两盒软妹~` 提交于 2019-12-03 14:29:31
In numpy if you want to calculate the sinus of each entry of a matrix (elementise) then a = numpy.arange(0,27,3).reshape(3,3) numpy.sin(a) will get the job done! If you want the power let's say to 2 of each entry a**2 will do it. But if you have a sparse matrix things seem more difficult. At least I haven't figured a way to do that besides iterating over each entry of a lil_matrix format and operate on it. I've found this question on SO and tried to adapt this answer but I was not succesful. The Goal is to calculate elementwise the squareroot (or the power to 1/2) of a scipy.sparse matrix of

byte operations (XOR) in python

有些话、适合烂在心里 提交于 2019-12-03 09:28:56
问题 #!/usr/bin/env python3 import binascii var=binascii.a2b_qp("hello") key=binascii.a2b_qp("supersecretkey")[:len(var)] print(binascii.b2a_qp(var)) print(binascii.b2a_qp(key)) #here i want to do an XOR operation on the bytes in var and key and place them in 'encryption': encryption=var XOR key print(binascii.b2a_qp(encrypted)) If someone could enlighten me on how I could accomplish this I would be very happy. Very new to the whole data-type conversions so yeah...reading through the python wiki

A loopless 3D matrix multiplication in python

不想你离开。 提交于 2019-12-03 02:54:43
I am looking to do the following operation in python (numpy). Matrix A is M x N x R Matrix B is N x 1 x R Matrix multiply AB = C, where C is a M x 1 x R matrix. Essentially each M x N layer of A (R of them) is matrix multiplied independently by each N x 1 vector in B. I am sure this is a one-liner. I have been trying to use tensordot(), but I that seems to be giving me answers that I don't expect. I have been programming in Igor Pro for nearly 10 years, and I am now trying to convert pages of it over to python. Sorry for the necromancy, but this answer can be substantially improved upon, using

Basic Numpy array value assignment

偶尔善良 提交于 2019-12-01 17:54:32
As a small exercise before i start playing with numeric code in python I am trying to make an LDLT algorithm. Just to "get the feet wet". However I seem to be lacking a fundamental understanding of the numpy array. See the following example: def ldlt(Matrix): import numpy (NRow, NCol) = Matrix.shape for col in range(NCol): Tmp = 1/Matrix[col,col] for D in range(col+1, NCol): Matrix[col,D] = Matrix[D,col]*Tmp if __name__ == '__main__': import numpy A = numpy.array([[2,-1,0],[-1,2,-1],[0,-1,2]]) ldlt(A) The example is not the full code I am working on. However, try and run it, and set a break

Basic Numpy array value assignment

谁都会走 提交于 2019-12-01 17:39:31
问题 As a small exercise before i start playing with numeric code in python I am trying to make an LDLT algorithm. Just to "get the feet wet". However I seem to be lacking a fundamental understanding of the numpy array. See the following example: def ldlt(Matrix): import numpy (NRow, NCol) = Matrix.shape for col in range(NCol): Tmp = 1/Matrix[col,col] for D in range(col+1, NCol): Matrix[col,D] = Matrix[D,col]*Tmp if __name__ == '__main__': import numpy A = numpy.array([[2,-1,0],[-1,2,-1],[0,-1,2]]

Unpredictable double [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-01 10:08:35
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Double precision problems on .NET Double calculation producing odd result I know that internal represantation of double value 0.2 is something like 0.199999 . But the following code still confuses me. CODE: public static void main(String[] args) { double d= 0.3d; double f= 0.1d; System.out.println(d+f); System.out.println(d*f); System.out.println(d); System.out.println(f); System.out.println(d-f); System.out

java Bit operations >>> shift

拟墨画扇 提交于 2019-12-01 03:52:58
Why if int x = -1 // binary: 11111111111111111111111111111111 x = x >>> 31; we have 00000000000000000000000000000001 but if int x = -1 x = x >>> 32; we have 11111111111111111111111111111111 (again -1) but not 00000000000000000000000000000000 ? From Section 15.19 of JLS : If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance . It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111) . The shift distance actually used is therefore