Applying a recursive function or variable-length loop in to elements of a numpy arrayPHP: Delete an element from an arrayUsing global variables in a functionDeleting array elements in JavaScript - delete vs spliceHow do you check if a variable is an array in JavaScript?Get the first element of an arrayLoop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?How can I add new array elements at the beginning of an array in Javascript?Loop through an array of strings in Bash?Swift for loop: for index, element in array?

New order #4: World

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

What are these boxed doors outside store fronts in New York?

What defenses are there against being summoned by the Gate spell?

My colleague's body is amazing

What typically incentivizes a professor to change jobs to a lower ranking university?

How can I fix this gap between bookcases I made?

Mean and Variance of Continuous Random Variable

Do airline pilots ever risk not hearing communication directed to them specifically, from traffic controllers?

What Brexit solution does the DUP want?

What does "enim et" mean?

Draw simple lines in Inkscape

Patience, young "Padovan"

What do you call a Matrix-like slowdown and camera movement effect?

How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)

What would happen to a modern skyscraper if it rains micro blackholes?

declaring a variable twice in IIFE

Why don't electron-positron collisions release infinite energy?

Non-Jewish family in an Orthodox Jewish Wedding

What is the white spray-pattern residue inside these Falcon Heavy nozzles?

Why is this code 6.5x slower with optimizations enabled?

How can bays and straits be determined in a procedurally generated map?

Can you lasso down a wizard who is using the Levitate spell?

Why CLRS example on residual networks does not follows its formula?



Applying a recursive function or variable-length loop in to elements of a numpy array


PHP: Delete an element from an arrayUsing global variables in a functionDeleting array elements in JavaScript - delete vs spliceHow do you check if a variable is an array in JavaScript?Get the first element of an arrayLoop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?How can I add new array elements at the beginning of an array in Javascript?Loop through an array of strings in Bash?Swift for loop: for index, element in array?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I've got a function of the form:



def f(x):

n = 0

while x > basecase:
x = g(x)
n += 1

return n


Basically, it applies function g() on x until a condition is met, and then returns how many applications n it took.



I'd like to be able to call this function on numpy arrays, so I can graph f(x) for a large range of x with MatPlotLib. But as the function is now, that doesn't work -- the comparison between x (an array) and basecase (a scalar) is a problem, as is the fact that n is a single integer and not an array.



I know I could just call vectorize, but that's slow -- basically the same as iterating manually. What can I do here? Are there any functional-programming numpy functions that can save me here, or would I have to rewrite things with nditer if I wanted to avoid the overhead of vectorize?










share|improve this question






















  • You can rewrite f() and g() using Cython; in any case, you will need to add additional condition to prevent infinity looping. Note: the iterative process you are applying to g(x) is finding root of equation x = g(x). This iterative process will not converge in some cases (this depends on g(x)).

    – bubble
    Mar 22 at 1:56











  • nditer is useful only as a stepping stone toward its use in cython. If you must iterate (as opposed to using whole-array numpy operations), work with lists.

    – hpaulj
    Mar 22 at 2:50











  • If x is an array, what would a while x>basecase mean? All values of x greater than the target? Any values greater? while is a scalar operation, yes/no. You need to be clear, more to yourself than us, what should happen.

    – hpaulj
    Mar 22 at 3:03











  • @bubble g(x) is known, and the process is guaranteed to converge. I just didn't want to include it for sake of simplicity. @hpaulj My goal is for the function to act exactly as if you fed it each element individually, so it tests each element separately and if the test fails, it loops. I'm not sure if something like that is possible (because you would kind of have to interleave python and C loops), which is why I'm asking.

    – a52
    Mar 22 at 3:39












  • So you aren't solving an array function, but rather a set of independent values. In other words, solving g for a bunch of different starting values.

    – hpaulj
    Mar 22 at 4:06

















0















I've got a function of the form:



def f(x):

n = 0

while x > basecase:
x = g(x)
n += 1

return n


Basically, it applies function g() on x until a condition is met, and then returns how many applications n it took.



I'd like to be able to call this function on numpy arrays, so I can graph f(x) for a large range of x with MatPlotLib. But as the function is now, that doesn't work -- the comparison between x (an array) and basecase (a scalar) is a problem, as is the fact that n is a single integer and not an array.



I know I could just call vectorize, but that's slow -- basically the same as iterating manually. What can I do here? Are there any functional-programming numpy functions that can save me here, or would I have to rewrite things with nditer if I wanted to avoid the overhead of vectorize?










share|improve this question






















  • You can rewrite f() and g() using Cython; in any case, you will need to add additional condition to prevent infinity looping. Note: the iterative process you are applying to g(x) is finding root of equation x = g(x). This iterative process will not converge in some cases (this depends on g(x)).

    – bubble
    Mar 22 at 1:56











  • nditer is useful only as a stepping stone toward its use in cython. If you must iterate (as opposed to using whole-array numpy operations), work with lists.

    – hpaulj
    Mar 22 at 2:50











  • If x is an array, what would a while x>basecase mean? All values of x greater than the target? Any values greater? while is a scalar operation, yes/no. You need to be clear, more to yourself than us, what should happen.

    – hpaulj
    Mar 22 at 3:03











  • @bubble g(x) is known, and the process is guaranteed to converge. I just didn't want to include it for sake of simplicity. @hpaulj My goal is for the function to act exactly as if you fed it each element individually, so it tests each element separately and if the test fails, it loops. I'm not sure if something like that is possible (because you would kind of have to interleave python and C loops), which is why I'm asking.

    – a52
    Mar 22 at 3:39












  • So you aren't solving an array function, but rather a set of independent values. In other words, solving g for a bunch of different starting values.

    – hpaulj
    Mar 22 at 4:06













0












0








0








I've got a function of the form:



def f(x):

n = 0

while x > basecase:
x = g(x)
n += 1

return n


Basically, it applies function g() on x until a condition is met, and then returns how many applications n it took.



I'd like to be able to call this function on numpy arrays, so I can graph f(x) for a large range of x with MatPlotLib. But as the function is now, that doesn't work -- the comparison between x (an array) and basecase (a scalar) is a problem, as is the fact that n is a single integer and not an array.



I know I could just call vectorize, but that's slow -- basically the same as iterating manually. What can I do here? Are there any functional-programming numpy functions that can save me here, or would I have to rewrite things with nditer if I wanted to avoid the overhead of vectorize?










share|improve this question














I've got a function of the form:



def f(x):

n = 0

while x > basecase:
x = g(x)
n += 1

return n


Basically, it applies function g() on x until a condition is met, and then returns how many applications n it took.



I'd like to be able to call this function on numpy arrays, so I can graph f(x) for a large range of x with MatPlotLib. But as the function is now, that doesn't work -- the comparison between x (an array) and basecase (a scalar) is a problem, as is the fact that n is a single integer and not an array.



I know I could just call vectorize, but that's slow -- basically the same as iterating manually. What can I do here? Are there any functional-programming numpy functions that can save me here, or would I have to rewrite things with nditer if I wanted to avoid the overhead of vectorize?







python arrays numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 0:57









a52a52

567




567












  • You can rewrite f() and g() using Cython; in any case, you will need to add additional condition to prevent infinity looping. Note: the iterative process you are applying to g(x) is finding root of equation x = g(x). This iterative process will not converge in some cases (this depends on g(x)).

    – bubble
    Mar 22 at 1:56











  • nditer is useful only as a stepping stone toward its use in cython. If you must iterate (as opposed to using whole-array numpy operations), work with lists.

    – hpaulj
    Mar 22 at 2:50











  • If x is an array, what would a while x>basecase mean? All values of x greater than the target? Any values greater? while is a scalar operation, yes/no. You need to be clear, more to yourself than us, what should happen.

    – hpaulj
    Mar 22 at 3:03











  • @bubble g(x) is known, and the process is guaranteed to converge. I just didn't want to include it for sake of simplicity. @hpaulj My goal is for the function to act exactly as if you fed it each element individually, so it tests each element separately and if the test fails, it loops. I'm not sure if something like that is possible (because you would kind of have to interleave python and C loops), which is why I'm asking.

    – a52
    Mar 22 at 3:39












  • So you aren't solving an array function, but rather a set of independent values. In other words, solving g for a bunch of different starting values.

    – hpaulj
    Mar 22 at 4:06

















  • You can rewrite f() and g() using Cython; in any case, you will need to add additional condition to prevent infinity looping. Note: the iterative process you are applying to g(x) is finding root of equation x = g(x). This iterative process will not converge in some cases (this depends on g(x)).

    – bubble
    Mar 22 at 1:56











  • nditer is useful only as a stepping stone toward its use in cython. If you must iterate (as opposed to using whole-array numpy operations), work with lists.

    – hpaulj
    Mar 22 at 2:50











  • If x is an array, what would a while x>basecase mean? All values of x greater than the target? Any values greater? while is a scalar operation, yes/no. You need to be clear, more to yourself than us, what should happen.

    – hpaulj
    Mar 22 at 3:03











  • @bubble g(x) is known, and the process is guaranteed to converge. I just didn't want to include it for sake of simplicity. @hpaulj My goal is for the function to act exactly as if you fed it each element individually, so it tests each element separately and if the test fails, it loops. I'm not sure if something like that is possible (because you would kind of have to interleave python and C loops), which is why I'm asking.

    – a52
    Mar 22 at 3:39












  • So you aren't solving an array function, but rather a set of independent values. In other words, solving g for a bunch of different starting values.

    – hpaulj
    Mar 22 at 4:06
















You can rewrite f() and g() using Cython; in any case, you will need to add additional condition to prevent infinity looping. Note: the iterative process you are applying to g(x) is finding root of equation x = g(x). This iterative process will not converge in some cases (this depends on g(x)).

– bubble
Mar 22 at 1:56





You can rewrite f() and g() using Cython; in any case, you will need to add additional condition to prevent infinity looping. Note: the iterative process you are applying to g(x) is finding root of equation x = g(x). This iterative process will not converge in some cases (this depends on g(x)).

– bubble
Mar 22 at 1:56













nditer is useful only as a stepping stone toward its use in cython. If you must iterate (as opposed to using whole-array numpy operations), work with lists.

– hpaulj
Mar 22 at 2:50





nditer is useful only as a stepping stone toward its use in cython. If you must iterate (as opposed to using whole-array numpy operations), work with lists.

– hpaulj
Mar 22 at 2:50













If x is an array, what would a while x>basecase mean? All values of x greater than the target? Any values greater? while is a scalar operation, yes/no. You need to be clear, more to yourself than us, what should happen.

– hpaulj
Mar 22 at 3:03





If x is an array, what would a while x>basecase mean? All values of x greater than the target? Any values greater? while is a scalar operation, yes/no. You need to be clear, more to yourself than us, what should happen.

– hpaulj
Mar 22 at 3:03













@bubble g(x) is known, and the process is guaranteed to converge. I just didn't want to include it for sake of simplicity. @hpaulj My goal is for the function to act exactly as if you fed it each element individually, so it tests each element separately and if the test fails, it loops. I'm not sure if something like that is possible (because you would kind of have to interleave python and C loops), which is why I'm asking.

– a52
Mar 22 at 3:39






@bubble g(x) is known, and the process is guaranteed to converge. I just didn't want to include it for sake of simplicity. @hpaulj My goal is for the function to act exactly as if you fed it each element individually, so it tests each element separately and if the test fails, it loops. I'm not sure if something like that is possible (because you would kind of have to interleave python and C loops), which is why I'm asking.

– a52
Mar 22 at 3:39














So you aren't solving an array function, but rather a set of independent values. In other words, solving g for a bunch of different starting values.

– hpaulj
Mar 22 at 4:06





So you aren't solving an array function, but rather a set of independent values. In other words, solving g for a bunch of different starting values.

– hpaulj
Mar 22 at 4:06












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55291395%2fapplying-a-recursive-function-or-variable-length-loop-in-to-elements-of-a-numpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55291395%2fapplying-a-recursive-function-or-variable-length-loop-in-to-elements-of-a-numpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현