How can Matlab or Octave be so fast? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am really confused about the computation speed of Matlab or Octave.
How is it possible to give the result of a computation like 5^5^5^5 (= 2.351*10^87 if you wanna know) instantly?
I found some results about the speed for matrix computations (this article), but nothing about other matters. And this is not the explanation (my (naive) implementation in Python is running for about 5 minutes right now).

5^5^5^5 doesn't require so many operations after all. For example, at each power step, say a^b, you can compute exp(log(a)*b), which gives the same result.
I'm not saying this is necessarily how Matlab does it, and there may be numerical precision issues. But this illustrates that a multiple-power operation is not so hard as its direct computation would suggest.
As for numerical precision:
>> format long
>> 5^5^5^5
ans =
2.350988701644576e+087
>> exp(log(exp(log(exp(log(5)*5))*5))*5)
ans =
2.350988701644561e+087
The relative error is
>> 1 - (5^5^5^5 / exp(log(exp(log(exp(log(5)*5))*5))*5))
ans =
-6.661338147750939e-015
which is not very far from eps.

Related

TSP [Nearest Neighbour] - Given The distances Matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Mates,
I need to solve a TSP Problem, with the distances info given as follows
I was trying to solve it by using dictionaries but I can't figure it out:
**Actually, dont even know how to post code as text here (just got 2 days with Python)
Any advide/help would be more than wellcome.
Thanks!
You can do sth like this:
import numpy as np
TT= np.array([[0,5,2,13,4],
[3,0,6,3,14],
[2,6,0,4,5],
[2,3,7,0,8],
[4,2,5,5,0]])
# assume we start in node 0
currentStop = 0
routeList = [0]
for _i in range(len(TT)-1):
TT[:,currentStop] = 100000 # Set column of visited stop to very large number
currentStop = np.argmin(TT[currentStop,:])
routelist.append(currentStop)
print(routelist)
TSP is an NP-hard problem, which means it's computationally hard to obtain the optimal result. It is possible to try all routes when the data size is small, but in practice we have thousands of nodes (nodes are locations in a TSP problem). Then we would use different searching algorithms to obtain a sub-optimal solution in reasonable time.
Since you are using Python I'd suggest you take a look at ortools. You can take a look at their simple examples to get started. It is written in C with a Python wrapper so it is much faster than pure Python.

Can dividing code too much make it inefficient? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
If code is divided into too many segments, can this make the program slow?
For example - Creating a separate file for just a single function.
In my case, I'm using Python, and suppose there are two functions that I need in the main.py file. If I placed them in different files (just containing the function).
(Suppose) Also, If I'm using the same library for the two functions and I've divided the functions into separate files.
How can this affect efficiency? (Machine performance-wise and Team-wise).
It depends on the language, the framework you use etc. However, dividing the code too much can make it unreadable, which is (most of the time) the bigger problem. Since most of the time you will (or should) be working in a team, you should consider how readable your code would be for them.
However, answering this in a definite way is difficult. You should ask a Senior developer on your team for guidelines.

How to calculate powers of complex numbers in Python without using the complex numbers data type [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm wondering how I can calculate powers of a complex number without using the complex numbers data type. So I have a function
def Power_complex(re, im, n):
How can I calculate (re + im * i)^n with this? Thank you!
You can use the Biniomial theorem for arbitrary exponents, although positive integers is the easiest case.
Or you can treat the problem in polar coordinates (this link simply gives you the answer, only click if you really don't want to figure it out on your own)

Array vs object - what's faster in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am wondering what I should do for the purpose of my project.
I am gonna operate on about 100 000 rows, every time.
what I wanted to do is to create an object "{}" and then, if I need to search for a value, just call it , for example
data['2018']['09']['Marketing']['AccountName']
the second option is to pull everyting into an array "[]" and in case I need to pull value, I will create a function to go through the array and sum numbers for specific parameters.
But don't know which method is faster.
Will be thankful if you can shed some light on this
Thanks in advance,
If performance (speed) is an issue, Python might not be the ideal choice...
Otherwise:
Might I suggest the use of a proper database, such as SQLLite (which comes shipped with Python).
And maybe SQLAlchemy as an abstraction layer. (https://docs.sqlalchemy.org/en/latest/orm/tutorial.html)
After all, they were made exactly for this kind of tasks.
If that seems overkill: Have a look at Pandas.

fill this array with another arrays elements with genetic algorithm in pyevolve [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
for example:
a=1500
b=[500,400,200]
One answer is:
ans=[1,2,1]
because 1*500+2*400+1*200=1500 I want to write a program with genetic algorithm with best evaluation function to solve this problem with this array with pyevolve python evolutionary tool.
Assuming that the coefficients in the answer must be integers, what you're describing is a linear Diophantine equation. It's not a good fit for a genetic algorithm, as the solution space is neither continuous nor smooth. (That is, there is not always a possible input between any two other inputs, and the "correct" answer will not necessarily be anywhere near other nearly-correct inputs.)
(If the coefficients in the answer can be real numbers, finding a solution is trivial to the point that a genetic algorithm would be overkill.)

Categories