Using `foreign import prim` with a C function using STG calling convention

Question: I have a simple C routine that takes four words and returns four words, and for which gcc can optimize and emit some primops that GHC doesn't support. I'm trying to benchmark various  ways of calling this procedure, and am having trouble trying to adapt the technique described here to use foreign import prim.
The following is meant to just add 1 to each input word, but segfaults.
Main.hs:
{-# LANGUAGE GHCForeignImportPrim #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples  #-}
{-# LANGUAGE UnliftedFFITypes #-}
import Foreign.C
import GHC.Prim
import GHC.Int
import GHC.Word

foreign import prim "sipRound"
  sipRound_c# :: Word# -> Word# -> Word# -> Word# -> (# Word#, Word#, Word#, Word# #)

sipRound_c ::  Word64 -> Word64 -> Word64 -> Word64 -> (Word64, Word64, Word64, Word64)
sipRound_c (W64# v0) (W64# v1) (W64# v2) (W64# v3) = case sipRound_c# v0 v1 v2 v3 of
  (# v0', v1', v2', v3' #) -> (W64# v0', W64# v1', W64# v2', W64# v3')

main = do
  print $ sipRound_c 1 2 3 4

sip.c:
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

// define a function pointer type that matches the STG calling convention
typedef void (*HsCall)(int64_t*, int64_t*, int64_t*, int64_t, int64_t, int64_t, int64_t,
                       int64_t, int64_t, int64_t*, float, float, float, float, double, double);

extern void
sipRound(
    int64_t* restrict baseReg,
    int64_t* restrict sp,
    int64_t* restrict hp,

    uint64_t v0, // R1
    uint64_t v1, // R2
    uint64_t v2, // R3
    uint64_t v3, // R4
    int64_t r5,
    int64_t r6,

    int64_t* restrict spLim,
    float f1,
    float f2,
    float f3,
    float f4,
    double d1,
    double d2)
{

    v0 += 1;
    v1 += 1;
    v2 += 1;
    v3 += 1;

    // create undefined variables, clang will emit these as a llvm undef literal
    const int64_t iUndef;
    const float fUndef;
    const double dUndef;

    const HsCall fun = (HsCall)sp[0];
    return fun(
            baseReg,
            sp,
            hp,

            v0,
            v1,
            v2,
            v3,
            iUndef,
            iUndef,

            spLim,
            fUndef,
            fUndef,
            fUndef,
            fUndef,
            dUndef,
            dUndef);
}

I don't really know what I'm doing. Is there a way to adapt the technique from that blog post? And is this a bad idea?
Comment: Right, this won't work. As the blog post says: "This is still a ccall function but we'll fix that later. There is currently no way to define this as cc10 (LLVM's internal name for GHC's calling convention) in clang." The C calling convention is different from GHC's. For example C thinks the first argument `baseReg` should be in `rdi` (assuming x86_64) but GHC is passing `baseReg` in `r13`.
Comment: This is very, very low level. Do you really need this level of performance? AFAICS, the blog post generates LLVM using clang, then patches the output by changing the C call convention to LLVM cc10 (the GHC one), and then compiler the result with llc. Scary. This is way beyond by comfort zone (i.e., I have very little knowledge of what's going on at this level), but using the cc10 calling convention seems to be crucial!
Comment: @chi I'm in the process of also benchmarking returning a struct of 4 words from a normal foreign ccall, but I'm expecting the overhead to be too great to be worth it (but might be surprised); I'm going through all this to try to get rotate instructions generated when not using the LLVM backend, for a library I'm working on. But this is also out of curiousity
Answer: If you're willing to hand-write assembly you can do it like this (for x86_64). Put this in a file with a .s extension and provide it as an argument on the ghc command line.
.global sipRound
sipRound:
    inc %rbx
    inc %r14
    inc %rsi
    inc %rdi
    jmp *(%rbp)

The mapping between STG registers and machine registers is defined in https://github.com/ghc/ghc/blob/master/includes/stg/MachRegs.h#L159.
Note that there will still be a function call involved, so it won't be as efficient as the code you are getting from LLVM.
Comment: Yeah it's possible for the C compiler to realize that a prologue is unnecessary and not insert one, but it's not guaranteed to work in general.
Comment: You can't use `void sipRound() { asm ( ... )}` since that will add a C function prologue. Just provide the assembler input like any other input. The function call I am talking about is the call you write to `sipRound_c#`. The point is that GHC can't "inline" `sipRound`, since it is implemented in assembly. (By contrast when you call `+#`, GHC doesn't generate a call to top-level function; it emits an add instruction.)
Comment: The ABI doesn't change much but it's not guaranteed to be stable; there's no ABI compatibility between different versions of GHC and each version of GHC only works with one version of LLVM, so it wouldn't be hard to get the modified calling convention into the next version of LLVM.
Comment: Just posted a followup question: http://stackoverflow.com/questions/41528208/why-is-there-no-foreign-import-prim-unsafe
Comment: I understand this much better now, thanks! Two quick followups: to expose this to ghc do we need to use inline asm in a c file, like `void sipRound() { asm ( ... )}` or is there a better way to do it (and is that why you say "there will be a function call involved"?)? And can I consider this a stable API? It looks as though that mapping hasn't changed much, and has to stay stable to coordinate with llvm etc.
Comment: Also, I'm curious whether you find this is actually faster for you than a regular FFI call.
Comment: Thanks, I'll try to update here after measuring both ways
Comment: And `void sipRound() { asm ( ... )}` did seem to work for me for whatever reason, but thanks for the edit!
Comment: I'm measuring 6 ns per iteration for the haskell function wrapping the assembly translation of a [SIPROUND](https://github.com/veorq/SipHash/blob/93ca99dcfa6a32b1b617e9a5c3c044685254ce8e/siphash.c#L44) using this approach, so I am going to be lazy for now and not benchmark allocating a struct, peeking in haskell-land, etc. This is a much nicer interface to work with anyway!


Como fazer Ckeditor ser obrigatório o preenchimento como um atributo required

Question: Não consigo fazer o ckeditor se comportar como um campo input ou textarea com required

<textarea data-toggle="tooltip" maxlenfth="200" data-maxlen="200" class="form-control" rows="2" id="cumprimentos" name="cumprimentos" required>
</textarea>

Comment: Qual versão vc está usando?
Comment: Ckeditor 4.12.1
Answer: Primeiro você tem que atribuir o plugin a uma variável:
var editor = CKEDITOR.replace('cumprimentos', {...

Veja que agora a instância do CKEditor tem um nome, editor.
Em seguida você usa o evento required do editor. Se o campo estiver vazio, irá disparar o alert e cancelar o envio do formulário:
editor.on('required', function(e){
    alert("preencha o campo");
    e.cancel();
});

Comment: Mas, Sam, mesmo assim o form é submitado e dá erro. Apesar de ter dado o alert.
Comment: Na verdade o input submit do formulário fica disable depois do alert.
Answer: Veja o exemplo funcional em
https://jsfiddle.net/jorgercosta/306gfwy5/10/
Utilizando o evento 'required'


Shutdown Windows Server and the corresponding machine

Question: I have read that shutdown -s in Windows Server only shuts down Windows. I don't know exactly what this means, but how can I shutdown and power off the whole machine?
Answer: If shutdown -s doesn't power down your machine you'll have to physically power it off or get in with some form of remote admin tool (like HP's iLO) and send a power down command.
Most servers nowadays, though, will power down with a shutdown.
Comment: It's an old server, I want to be prepared:)Does it matter if I do shutdown -s and push the power button?
Comment: @DarkFire21 Might want to be prepared to push the Off button, then.  Push the power button once you get the "Windows is not shutdown, you can turn off your machine" message on the screen.
Comment: Ok, so I run the command and when the above message appears I push the power button.Sorry for my noobiness but it's the first I have to administer such an important machine, alla alone.thanks:)
Comment: @DarkFirew21 No need to apologize, that's why we're here :)
Comment: Oh, and the above should read "Windows is NOW shutdown, you can...."  Oops
Comment: It's been most then 10 years since I've seen a server not power off. Bios has to support APCI and you need an ATX style power supply. both have been around for 12+ years. even NT 4.0 can down a box...  The question we should ask is why do you have to power down this server?
Answer: shutdown -s is the same as doing Start>Shutdown. It takes a REALLY old server to require you to also hit the power button after Windows is shutdown. If your server is that old, it probably total junk.


Placing elements next to each other without wrapper div

Question: I'm currently styling a WordPress site's contact form. The desired result should look something like this:

Normally, this could be easily achieved with a wrapper div around the text fields. Since this is WordPress, I can't do this without digging into the plugin (which is the last thing I want to do). The HTML WordPress produces looks like this:
<form ...>

    Naam Achternaam
    <input type="text" ...>

    Bedrijf
    <input type="text" ...>

    E-mail
    <input type="email" ...>

    Telefoonnummer
    <input type="text" ...>

    Vragen / opmerkingen
    <textarea ...></textarea>

  ...
</form>

which currently looks like this:

Is it possible to achieve what I'm after with this HTML structure or do I need to find a way to accommodate a wrapper div?
Comment: Can you please create a fiddle?
Comment: @RobertWilliams Sure thing. Give me a sec.
Comment: @RobertWilliams https://jsfiddle.net/gbrmqswf/
Comment: Did @ovokuro's answer worked for you?
Comment: @RobertWilliams It works, but in an ideal world, you wouldn't want to resort to `position: absolute`. If that is the only possibility, it'll have to do.
Answer: You could use position: absolute on the last p... not the nicest solution but a possible workaround without changing the HTML

form {
  background: #00CDD4;
  display: flex;
  flex-direction: column;
  position: relative;
}

form p {
  margin: 1em;
  display: flex;
  flex-direction: column;
  width: 45%;
}

form p label {
  text-transform: uppercase;
  font-family: sans-serif;
  opacity: .87;
}

form p input {
  border: none;
  padding: 1em;
}

form p:last-of-type {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
}

form p:last-of-type textarea {
  height: 100%;
}
<form>

    Naam Achternaam
    <input type="text" ...>

    Bedrijf
    <input type="text" ...>

    E-mail
    <input type="email" ...>

    Telefoonnummer
    <input type="text" ...>

    Vragen / opmerkingen
    <textarea ...></textarea>

</form>
Comment: Thanks, I guess there isn't really an alternative to absolute.
Answer: I tried alot but following your requirements it seems only possible through position absolute. Just for fun I tried to make without display: flex;

body {
  background-color: #2dccd3;
  font-family: 'Montserrat', sans-serif;
}

form.contact{
  position: relative;
}

p.contact-field {
  width: 45%;
}

p.contact-field input{
  width: 100%;
}

input, label {
    display:block;
}

p.contact-field:last-child{
  margin: 0px;
}

.contact-field:last-child{
  position: absolute;
  right: 0px;
  top: 0px;
  width: 50%;
  height: 89%;
}

textarea#message{
  height: 100%;
}
<form class="contact">

    Naam Achternaam
    <input id="name" type="text" name="name">

    Bedrijf
    <input id="company" type="text" name="company">

    E-mail
    <input id="email" type="email" name="email">

    Telefoonnummer
    <input id="phone" type="text" name="phone">

    Vragen / opmerkingen
    <textarea id="message" name="message"></textarea>

</form>


Bookings API versus existing spaghetti code

Question: I'm working on an appointment booking system that allows registered users to book an outcall with an agent.
I've only come across Bookings API and Public Bookings, which seems more for things like library work rooms than it is for service industry people -- and even then, nothing about it looks at all pretty (Not a knock to the devs; it just isn't at the point comparable software is at on other CMSes.). My client has an existent system coded in some pretty funky PHP -- nothing OO, all logic code is in the same file as the design code (which is in, get this -- tables. Everything. In tables.) -- and I'm currently thinking about porting it to Drupal.
My questions are thus:
a. Are there any alternatives to Bookings API for appointment booking?
b. If not -- and considering I don't have a ton of Drupal programming experience -- would it be more worthwhile to update the existing spaghetti code appointment booking system to function as a Drupal module, or more worthwhile to just make Bookings API function more eloquently in this usage case (considering it has a lot more functionality, hooks, etc. than I could probably program into a new module in a short time)?
Thank you!
Answer: In answer to part (a) there is the Resource Conflict module, which appears to be similar in goal to the modules you mention above.
As for part (b) I'd argue that it is typically better to update or replace custom spaghetti code in such a way to work with existing modules--doing so will greatly ease the work of future maintainers of your code, and allow it to benefit from future enhancements to the Bookings API.
Answer: Check the Booking Time Slots module, or see Comparison of Booking System modules.

Module provides Time Slots Booking functionality.
For example:

Users can go to Calendar and book specified time slots
You can configure how many time slots are available and how long are the events, so after that time slot will be released
You can configure hourly and half-hourly time lots (in calendar view)


How can I clear the trails left by moving an SVG line?

Question: In a web app, I want to join two HTML element with a smooth line, and allow the user to move either element. The line should continue to join the two elements. I have created a jsFiddle here which illustrates the concept.
Here's the basic code I use for drawing the line, using jQuery:
$(function ready() {
    var $body = $("body")
    var ns = "http://www.w3.org/2000/svg"

    var $path = $(document.createElementNS(ns, "path"))
    $path.attr({
        "d": "M 5 5 C 100 5, 100 195, 195 195"
      , "stroke-width": 10
      , "stroke-linecap": "round"
      , stroke: "black"
      , fill: "none"
    })

    var svg = document.createElementNS(ns, 'svg')
    var $svg = $(svg)
        .attr("version", "1.1")
        .attr("xmlns", "http://www.w3.org/2000/svg")
        .attr("baseProfile", "tiny")
        .attr("width", 200)
        .attr("height", 200)
        .append($path)

    $body.append($svg)
})

I use an SVG line between the moveable elements, and it leaves a trail each time it is redrawn. (This makes for very pretty feather-shaped designs, but that is not my purpose). How can I get the HTML page to refresh in the areas where the line is no longer present?
I know that if I were to do this using a canvas, I could copy the clean rect where the line is going to be drawn, then draw the line. Redrawing the line would consist of three steps:

replacing the clean image of the dirtied rect
copying the clean image of the rect that is about to be drawn on
drawing the line

I am guessing that, when working with SVG elements, the process is rather different. What should I do?
Answer: With SVG, a line is an object. Don't create a new line, move the existing one. Save $path, and do $path.attr('d', ".....") with the new coordinates.


Error "Public files directory Not fully protected" Appears in Nginx?

Question: I'm wondering why this errors appear on my drupal site when I am using Nginx:
<code>Public files directory  Not fully protected
See http://drupal.org/SA-CORE-2013-003 for information about the recommended .htaccess file which should be added to the sites/default/files directory to help protect against arbitrary code execution.
Error
Temporary files directory   Not fully protected
See http://drupal.org/SA-CORE-2013-003 for information about the recommended .htaccess file which should be added to the tmp directory to help protect against arbitrary code execution.
</code>
Any solution to this?
Answer: In the security advisory referenced in the error, it describes the specific vulnerability as "Code execution prevention (Files directory .htaccess for Apache - Drupal 6 and 7)" and recommends updates to your htaccess files in your files directory. Part of the recommended htaccess update is the following text:
<code># Set the catch-all handler to prevent scripts from being executed.
SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
<Files *>
  # Override the handler again if we're run later in the evaluation list.
  SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003
</files>
</code>
The SetHandler string is what Drupal is using to determine whether you've made this change or not.  Even though you're using Nginx as your webserver, its still going to display this error until you've made this update. From the advisory again:

To fix the code execution prevention vulnerability on existing Apache
  installations also requires changes to your site's .htaccess files in
  the files directories. Until you do this, your site's status report
  page at admin/reports/status will display error messages about the
  problem. Please note that if you are using a different web server such
  as Nginx the .htaccess files have no effect and you need to configure
  PHP execution protection yourself in the respective server
  configuration files.

I'm running a couple nginx servers as well and had to make these updates just to make these errors go away. I was curious how they were doing this check and found this in the
modules/system/system.install file at line 284:
<code>// Check for the string which was added to the recommended .htaccess file
// in the latest security update.
if (!file_exists($htaccess_file) || !($contents = @file_get_contents($htaccess_file))
|| strpos($contents, 'Drupal_Security_Do_Not_Remove_See_SA_2013_003') === FALSE) {
...
</code>
Comment: @jaypabs From the quote above: _Please note that if you are using a different web server such as Nginx the .htaccess files have no effect and you need to configure PHP execution protection yourself in the respective server configuration files._ so potentially your server is insecure, yes. You'll need to get your sysadmin to review the security advisories and check/make adjustments to your server config. If you're having problems with that please ask about it on SuperUser as we don't deal with server admin here. Thanks
Comment: Thank you. The error in status report has gone. Since I'm using Nginx, is my server safe from this vulnerability?
Comment: Just seems wrong to force this on all types of servers when it only works for apache, should be optional.
Answer: I made a core patch for users using non apache servers, you can find it here, maybe one day it goes into core
https://www.drupal.org/project/drupal/issues/3117665


Different in theme as table and page

Question:
I am studying the pager_example in example module.

When creating an render array, it first assign #theme as 'table'. But it assign the #theme as 'pager' just before returning.
What is the purpose of attach the pager theme ?
pager_example.module:
<code>// Create a render array ($build) which will be themed as a table with a
// pager.
$build['pager_table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => t('There are no date formats found in the db'),
);

// attach the pager theme
$build['pager_pager'] = array('#theme' => 'pager');  <---- why need this ?
return $build[];
</code>
Then I remove the coding "$build['pager_pager'] = array('#theme' => 'pager');". The result is the same.
When compare to tablesort_example.module, it don't have attach the pager theme before the return.
tablesort_example.module:
<code>// build the table for the nice output.
$build['tablesort_table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
);

return $build;
</code>
Answer: The code <code>$build['pager_pager'] = array('#theme' => 'pager');</code> returns the pager for the table.
The example module output with that line of code:

The example module output without that line of code:

If you are not seeing the same difference I am, it might be a cache issue, try clearing your cache between changes.
Comment: Thx Scott. You are awesome. I find the different after clearing the cache.

Different in theme as table and page


Display definition of the term searched for - above search results

Question: Beginning a search on google with 'what is psych'... triggers auto completion with 4 suggested phrases - arrow down through each and you will see its definition appear above its pertaining results:

How can I add a definition of the terms (if available) above the results? Ideally would like to use auto complete. I've attempted to achieve this with the Google custom search engine and Google site search modules. Unfortunately, the Google Dictionary service appears to no longer be supported by Google.
I explored another option such as using Solr search api and coding a second custom call from a definition source such as Wordnik and then combining that result with the Solr results. I think this may be feasible but not sure yet how will combine the results.
How could I combine search results to include a definition and/or is there a better way to include a definition of what the user may be searching for, above the actual results?
Answer: It turned out it was necessary to use Wordnik api (or similar) to get the definition service that I required. So I've written a module to handle the api and show definition/image for the searched terms if available.
After talking to Google support and verifying my research that google no longer provides a definition service (they did back in 2011) to private websites, I realized it best to abandon the idea of using the google custom search engine. Instead I've setup Solr which is a bit more work but much more custom.


Python type comparison table

Question: PHP has 'PHP type comparison tables' is there anything like that for Python?
Answer: Yes, here (also included image below):
https://i.stack.imgur.com/KSS8R.jpg
From https://twitter.com/ngkabra/status/449904315012878337
Comment: I would like to add that 0.0 == False.
Comment: Furthermore, of course, `float('nan') != float('nan')` -- so the diagonal is "broken" in some places.
Comment: Sorry it took two years :)
Answer: Python is strongly typed; such a table is not required except between basic numeric types and basic string types. For numeric types, they are coerced to long (in 2.x) or float as required. For string types, things are not as simple and so unicode (in 2.x) should be used where possible.
>>> 3 > 2.4
True
>>> 'a' >> u'a' < 'あ'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)

Comparison across incompatible types in 2.x will not act as you expect.
>>> 2 < '3'
True
>>> 3 < '2'
True

Comparison across incompatible types in 3.x will fail.
3>> 2 < '3'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()

Comment: "Python is dynamically typed. The Python compiler is blissfully unaware of the types used in a program, and all type checking is done at run time." - http://python-history.blogspot.ca/2009/02/pythons-use-of-dynamic-typing.html
Comment: Because of comparisons like you pointed out, or comparing int to methods (because brackets were omited) or plain old 0 == False, and other comparison nuances make python in my books not a strong typed language. Interestingly it appears Assert statements will assist in catching such gotchas early on: "Assertions are particularly useful in Python because of Python's powerful and flexible dynamic typing system." - http://wiki.python.org/moin/UsingAssertionsEffectively .

Looking back my question it was not specific enough so I apologize for that and appreciate your response.

Python type comparison table


What is the best test for validating trends in time-series data?

Question: I have annual temperature data from a variety of weather stations in the Caribbean and I want to be able to show statistically that the trends for each station are significant, either positive or negative or no trend.
I can obviosly plot the data and fit a line, but I want to make a stronger argument than just eyeballing it.  I was thinking about a runs's test or a Mann-Kendall test.
Most everything I've read talks about ARIMA models, concernes about autocorrelation, etc. but I feel like those are for econometric predictions.  I'm not trying to predict anything.  I just want to be able show whether or not the increase or decrease in temperature is due to random chance, or not. (my n=49)  I may have larger n values for other stations, but for right now, working with 1 station, I have 49 observations.  It would be nice to be give some idea of how far above or below random chance it is.
I don't know this helps, but I actually have monthly data, but to avoid periodicity issues I've so far been playing around with July values to represent N. Hemisphere summers.
In terms of hypothesis testing, if the Caribbean is not warming, then none of the weather stations would show increasing trends in temperature.
Comment: If you're going to step into the climate change controversies, you had better be prepared to do excellent statistical work, or you will be slaughtered by the critics.  In this case such work has to include (among other things) proper assessment of temporal correlation and careful justification of the time ranges chosen for analysis.  *That's* why you need ARIMA models or their equivalent and that's why a simple runs test or M-K is not going to pass muster.
Comment: I'm not necessarily advocating ARIMA; what is most important from your point of view is the assumption of *independence of the regression residuals.* Yours likely have strong serial correlation. That could completely change the hypothesis tests concerning significance of the slope; no matter what procedure you use, it would be invalid to ignore such correlation.  ARIMA is one of many methods to model and explicitly account for serial correlation.
Comment: This is just for a class so I don't need to worry too much about critics.  I can justify the time ranges to my professor.  Its the assessment of temporal correlation that has me bothered.  Since I'm not doing any predicting, I just would need to show that my ARIMA model for the observed data is accurate, and that the ARIMA model contains an increasing, or decreasing trend.  Effectively I would need to detrend the model, but in doing so, I'm justifying that there was a trend to begin with?
Comment: Not at all: detrending works even in the absence of trends; this operation is not an implicit admission that there's any trend. BTW, I added the [tag:homework] tag to this question to reflect its classroom nature--that ought to help deflect other comments about real-world applications.
Comment: So maybe this is where I need to do more reading.  How does an ARIMA model indidcate any trend is significant?  Maybe I am confusing decompostion and detrending.
Comment: If ARIMA will do the job, then that is what I shall use.  I'm at least familiar with the term ARIMA, which is a step above all other types of tests.  I know other folks around here have done work with ARIMA too, so that helps.  Thanks whuber.  I would normally ask my professor these types of questions but he has not been in his office today.
Answer: I use a method for temperature trends that relies on actual measurements rather than computed values such as monthly or annual means. First I de-diurnalize and de-seasonalize the data with a general linear model against dummy coded months and times of day. The residuals of this regression form the deseasonalized series in which I look for long term trends with a simple OLS linear regression against time measured in years. The regression coefficient for time is usually very small and it explains a very small portion of the sum of squares; so even if the regression coefficient is statistically significant, it is necessary to confirm the trend with some kind of robust methodology that is not sensitive to OLS assumptions. For an example of these computations please see:
https://www.academia.edu/14038781/A_ROBUST_TEST_FOR_OLS_TRENDS_IN_DAILY_TEMPERATURE_DATA


Constructive proof of boundedness of continuous functions

Question: Consider the theorem for the continuous function:
Let $a<b$ be real numbers, and let $f:[a,b]\to{\bf R}$ be a function continuous on $[a,b]$. Then $f$ is a bounded function.
The proof in the classical textbook on real analysis uses the Heine-Borel theorem. It dose not say how to find the bound for $f$, but it show that having $f$ unbounded leads to a contradiction.
Here are my questions:

Is there a direct [EDITED: constructive] proof for this theorem?

More generally, can a theorem in mathematics always have a constructive proof? Or what kind of statements do not have any constructive proof, say, one has to use techniques such as "proof by contradiction" in order to prove it?
Comment: Given the [Gödel-Gentzen transformation](http://en.wikipedia.org/wiki/G%C3%B6del%E2%80%93Gentzen_negative_translation) you could argue that any theorem in mathematics (ZF(C)) can be proved constructively.

You actually prove a classically equivalent statement (and it is not informative given that the final result is a negation) but...
Answer: There's a natural proof using sup. Consider the set $X=\{ x \in [a,b] : f \mbox{ is bounded in } [a,x] \}$. Then $X$ is non-empty and no $t<b$ is an upper bound for $X$. Hence $b=\sup X$ and $f$ is bounded in the whole interval. This argument is actually the same one used in Heine-Borel: for continuous functions in compact sets, locally bounded implies globally bounded. That $f$ is locally bounded comes from continuity and is used to prove that no $t<b$ is an upper bound for $X$.
Comment: Is this supposed to be a proof?
Comment: @Glen, yes. Or a sketch, rather. This proof is given in full in Spivak's *Calculus*, Theorem 7-2.
Comment: Excuse me ... i can prove that $b=supX$ but i can't prove $b \in X$ ... can you please help me ?
Comment: Indeed, is there a constructive proof that $b \in X$ ? By continuity, you can bound $f$ on $[b-\eta, b]$ and assert that $b-\eta$ is not $\sup X$. But does this allow you to assert that there constructively exists a point $y\in X$ above $b-\eta$ ?
Answer: There is no a-priori bound on the function: consider the constant function $f = M$. Also, the maximum can be attained at any given point - just take any appropriate quadratic.
As for the second question, that really depends on your notion of "directly".
In constructive mathematics, I believe that a continuous function is supplied with some "modulus of continuity" which immediately implies (directly) boundedness. Whether one can prove intuitionistically that the $\epsilon-\delta$ definition of continuity implies boundedness - probably not, but ask an expert!
Answer: As for your question about statements that can be proven "directly" or not, I find that this article by Tim Gowers and the resulting discussion in the comments is very interesting. It even addresses the Heine-Borel theorem.
Answer: Let $\{x_n\}_{n=1}^{\infty}$ be a sequence of points such that $|f(x_n)|$ converges to $M = \sup_x |f(x)|$. You can pass to a convergent subsequence $\{x_{n_k}\}_{k=1}^{\infty}$ which converges to some $y \in [a,b]$. Then by continuity of $f(x)$
$$|f(y)| = \lim_{k \rightarrow \infty} |f(x_{n_k})| = M$$
So $f(y) = \pm M$; therefore $M$ is finite and $|f(y)|$ achieves the value $M$. The proof of the existence of convergent subsequences as in the proof of the Bolzano-Weierstrass theorem, using divisions into halves and so on, is not by contradiction and is reasonably constructive.
Comment: I don't see how this is any more direct than the proof outlined in the OP.
Comment: @Qiaochu, @Zarrax:  I agree with Qiaochu here, this proof doesn't completely work for me.  It seems like you are saying this:  "Let $M$ be the supremum.  Then by sequence argument, and continuity, there exists $y$ such that $f(y)=M$.  But $f(y)\in\mathbb{R}$ so $M<\infty$."  Worded this way, it doesn't completely make sense, and is just a proof by contradiction in disguise.  I feel the $g(x)$ you made is just to disguise this further.
Comment: This is exactly the proof in Tim Gower's article, few posts above ;) And he makes an interesting point about it: how do you prove that $x_n$ exists? If $M$ is finite, there is nothing to prove, but if $M$ is infinite, the existence of $x_n$ is based on the assumption that $M$ is infinite, how is this different than contradiction?
Comment: it's not a proof by contradiction, the existence of a convergent subsequence is not proven in that way. This is what the poster asked for.
Comment: Also, the usual proof of the existence of convergent subsequences; i.e. repeated dividing into halves as in the Heine-Borel theorem, is appropriately constructive given the generality of the question.
Comment: I guess what I meant to say is, I don't see how this is a proof. How do you know that $g(y) > 1$?
Comment: $g(y)$ is always ${1 \over 1 + |f(y)|}$, that's all you need. It's continuous because $f$ is, and you don't need anything about the magnitude of $f$ or $g$.
Comment: I just realized you don't even need $g(y)$ here; I edited the proof accordingly.
Comment: Yes, your edit does exactly what I was thinking.  Beat me by 1 minute!
Comment: At any rate, I think I've made this clear enough; you're under no obligation to believe it.
Comment: @Zarrax: my apologies. Your use of $g$ was confusing; it didn't seem necessary and I couldn't figure out if you were using it to hide a step or what. It's fine now.
Comment: This is more a phylosophical question but suppose we prove $P \Rightarrow Q$ by contradiction. Is the following proof the same, or is it constructive? We break the proof in two cases: Case 1: Q is true (nothing to prove)..... Case 2: Q is false... We show that this is not possible....
Comment: @user9176: I'm breaking into infinitely many cases really, for each $M$ I am constructing a sequence of $x_n$ where $|f(x_n)| > M - {1 \over n}$. If $M$ is infinity then $|f(x_n)| > n$. Basically all that's going on here is: I have a sequence $a_k = |f(x_{n_k})|$ that either converges to a finite limit or infinity. Then I show using continuity that it's a finite limit. There's no proof by contradiction; at no point do I assume $M$ is infinity. The definition of the $x_k$ might be via cases in the above sense... but that's not the same as a proof by contradiction.
Comment: One other thing.. if the fact that it's infinity that is worrying you, you can define $x_n$ in terms of $g(x) = {1 \over 1 + |f(x)|}$ as I did before. Although this adds a layer of complication that bothered people above, this more graphically indicates why "infinity doesn't matter here". I found Gowers's blog post, and I really do not agree with him. But it does explain why I got such a response to what I thought was a relatively uncontroversial posting :)
Comment: Nice Idea. Then your proof can be simplified nicely the following way: Step 1: prove the weaker result If $f$ is continuous and bounded on $[0,1]$ then $f$ attains its maximum and minimum. Step 2: To get the result in general, apply Theorem from Step 1 to your function $g$, or maybe to $\arctan(f(x))$. Since the new function attains the maximul, it means $f$ attains its maximum thus it is bounded.....
Answer: Would this be considered a direct proof?
Pick $x_n$ a sequence which is dense in $[0,1]$. Let $y_n =f(x_n)$ and $z_n =\max_{1 \leq i \leq n} y_i$.
Then $z_n$ is increasing, and thus has a limit.
Let $A:= \{ m | z_m > z_{m-1} \}$. If $A$ is finite, it has a maximum $k$ and since $y_n \leq z_k$ for each $n$, it follows that $z_m$ is the maximum of $f(x)$.
If $A$ is infinite, then order its elements incresingly $n_1 < n_2 < ..< n_k< ...$. Then by the definition of $A$, the sequence $y_{n_k}$ is increasing and
$$y_{n_k}=z_{n_k}= \max_{1 \leq i \leq n_k} y_i (*) \,.$$
Finally pick a convergent subsequence $x_{m_l}$ of $x_{n_k}$. Let $a$ be the limit of this.
By continuity
$$\lim_{l \to \infty} y_{m_l}= f(a) \,.$$
Since $y_{m_l}$ is a subsequence of $y_{n_k}$, and $y_{n_k}$ is increasing we get
$$\lim_{k \to \infty} y_{n_k}= f(a) \,.$$
But then, since $y_{n_k}$ is increasing, we get from $(*)$ that $y_n \leq f(a)$ for all $n$. Now the density of $\{ x_n \}$ completes the proof.
Note We actually used Heine-Borel theorem in the proof, when we picked a convergent subsequence of $x_{k_n}$ (unless of course one defines compactness that way). No matter what proof one tries, since the same theoremfails for $(0,1)$, somehow compactness is bounded to come in play....
Answer: Choose $x_n=\inf\{x:|f(x)|>n\}$, and note that this is bounded by $b$ and (non-strictly) increasing by construction. Now consider:
$$\sup_n {x_n}=\lim_{n \rightarrow \infty} x_n=x_\infty$$
Since $f$ is a real valued function also defined on the end points, there is some $M$ such that $f(x_\infty)=M-1$. Furthermore, since $f$ is continuous
$$f(x_\infty)=\lim_{n \rightarrow \infty}f(x_n)=M-1.$$
Which concludes the proof. However, I would not consider this constructively acceptable since the least upper bound property is not constructively acceptable and we did not even construct the supremum. This is a direct proof, however.
Constructively the theorem may not even be true or relevant. According to Brouwer every function is uniformly continuous, while in Russian recursive mathematics it is possible to disprove the claim by use of Specker sequence. Hence Bishop's analysis (consistent with set theory) limits itself to uniformly continuous functions and the claim is unproved.


How to do Jumps while sight reading

Question: I am a total beginner at piano sight reading. I find it difficult to change chords or move from one octave to another while looking at the sheet without pressing the wrong notes. Any advice?? thanks.
Comment: This is a good, valid question, but the title at this point in time doesn't make any sense to me.
Answer: Apart from years of practice, which will bring this sort of ability out naturally (eventually), there are a few things you can do in order to help the process along:
One is learning to play scales while blindfolded or in the dark, so that you cannot see the keys at all. I would recommend starting with B-Major which uses all the black keys and just 2 white keys in each octave. You can start by finding the 3 black notes adjacent to each other - by touch, entirely without looking, and then finding the 2 black keys adjacent to each other (C and D sharp). then play a one octave scale blind, then move on to all the other keys.
Extend this by trying to play all the arpeggios without looking at the keys.
Try playing scales in octaves with one hand, using just fingers 1 and 5. Allow yourself to look at the keys at the start of the scale, but play the rest blind.
Take a book of pieces that you could comfortably play a year ago, and play them again while looking ONLY at the sheet music.
You might also try to develop a sort of peripheral-vision where look mostly at the sheet music but keeping aware of where your fingers are located on the keyboard.
Eventually, you will gain a kind of feel for the geography of the keyboard which makes it easier to find the jumps and leaps without taking your whole attention away from the sheet music.
Some thing else which develops over time is the ability to "read ahead" in the sheet music so that having read a few notes ahead of where you are playing, you have a split second to get a quick glance at the keys and then back to the music without falling behind.
Finally, don't get obsessed with all this, as it will happen naturally anyway. Just spend a few minutes each day trying some of the above techniques, and see if they help you - but it will still take time for improvements to show.
Comment: so you are saying with time I will be able to play with my eyes closed???
Comment: Yes, @n00B, it is certainly possible to learn to play with eyes closed, after all, there are blind piano players! That isn't the main aim; the real aim is to cut down the amount of time spent looking at your fingers so that you spend almost all your time reading the notes with just glances now and then at the keyboard to make sure.
Comment: @n00B Just as with a typing class, you will need to start practicing very small simple riffs of only a few keys without looking at the keyboard - it may even be helpful to block your view of your hands and stumbling along "blindly" for a few days. Combining that with enough repetition of a given piece to memorize it, playing with your eyes closed actually isn't difficult. While not a meaningful goal on it's own, closing your eyes shouldn't much affect your ability to get your hands in the right place for beginner or even intermediate pieces.
Answer: Learning piano or guitar or trombone or anything else involves practice. Small children use the same technique to learn to walk and talk, they just keep at it till they get it right. they stand up and fall down so many  times you  think they'll never get it right. but every time they stand up something is developing in their brain that helps them the next time they try. they have an interest in learning to stand, walk, and talk. That's the same process as learning anything, trying and failing is just what needs to happen till your brain gets developed to the point that  it can do what you're trying to do. you may find it boring or perhaps it's enjoyable, usually dependent on your interest level.
It must be said that proper technique is the way to go because poor technique
is learned as effectively as good technique, but the results usually leave a lot to be desired. We're cheering for you!

How to do Jumps while sight reading


CrawlSpider not following links

Question: Title says it all, I'm trying to make a CrawlSpider work for some products in Amazon to no avail.
Here is the original URL page I want to get products from.
Looking at the HTML code where the next link is, looks like this:

    Next Page

This is the current reg expression I'm using:
s/ref=sr_pg_[0-9]\?[^">]+

And using a service like Pythex.org, this seems to be ok, I'm getting this portion of the URL:
s/ref=sr_pg_2?me=A1COIXT69Y8KR&rh=i%3Amerchant-items&page=2&ie=UTF8&qid=1444414650

Here is the code of my crawler:
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from amazon.items import AmazonProduct

class AmazonCrawlerSpider(CrawlSpider):
    name = 'amazon_crawler'
    allowed_domains = ['amazon.com']
    #allowed_domains = ['stackoverflow.com']
    start_urls = ['http://www.amazon.com/s?ie=UTF8&me=A19COJAJDNQSRP&page=1']
    #start_urls = ['http://stackoverflow.com/questions?pagesize=50&sort=newest']
    rules = [
        Rule(LinkExtractor(allow=r's/ref=sr_pg_[0-9]\?[^">]+'),
           callback='parse_item', follow=True)
    ]
    '''rules = [
        Rule(LinkExtractor(allow=r'questions\?page=[0-9]&sort=newest'),
           callback='parse_item', follow=True)
    ]'''

    def parse_item(self, response):
        products = response.xpath('//div[@class="summary"]/h3')
        for product in products:
            item = AmazonProduct()
            print('found it!')
            yield item

For some unknown reason, the crawler is not following the links. This code is based on the blog tutorial from the guys at RealPython, where they crawl StackOverflow for questions. Actually, just uncomment the commented code to see that this works.
Any idea what I'm missing here? Thanks!
UPDATE:
Based on the answer from @Rejected, I've switched to shell and I could see that the HTML code is different, as he pointed out, than the one I could see in the browser.
Actually, the code Scrapy is getting, the interesting bits, is:

    Next Page

I've changed my reg expression so it looks like this:
s[^">&]+&me=A19COJAJDNQSRP&page=[0-9]$

Now I'm getting the links in the shell:
[Link(url='http://www.amazon.com/s?ie=UTF8&me=A19COJAJDNQSRP&page=1', text='\n    \n        \n            \n            \n            \n            \n    \n    ', fragment='', nofollow=False), Link(url='http://www.amazon.com/s?ie=UTF8&me=A19COJAJDNQSRP&page=2', text='2', fragment='', nofollow=False), Link(url='http://www.amazon.com/s?ie=UTF8&me=A19COJAJDNQSRP&page=3', text='3', fragment='', nofollow=False)]

And also the crawler is getting them correctly!
Answer: Scrapy is being provided different HTML data than what you are seeing in your browser (even just requesting "view-source:url").
Why, I wasn't able to determine with 100% certainty.  The desired three(?) links will match r's/ref=sr_pg_[0-9]' in your allow path.
Since Amazon is doing something to determine browser, you should test what you're getting in your instance of Scrapy, too.  Drop it into shell, and play around with the LinkExtractor yourself via the following:
LinkExtractor(allow=r's/ref=sr_pg_[0-9]').extract_links(response)

Comment: Hello @Rejected, this returns null in my case. I'm getting '[]' to the command you posted in the shell. But you pointed me in the right direction, thanks! I'll edit the question to see the results :)
Comment: Yeah, I had a Useragent setup, which was likely different than yours.  Amazon is doing *something* which is causing these dynamically generated links to be different for each of us.  Best of luck moving forward.

CrawlSpider not following links


Sqlite order by time closest to now

Question: My sqlite raw query needs to be able to sort dates based on how close they are to now. What is wrong with my sqlite statement?
public Cursor GetFirstTime(){
    Calendar c = Calendar.getInstance();
    int seconds = c.get(Calendar.MILLISECOND);
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    Cursor nextTime = sqLiteDatabase.rawQuery("select  * from timemanager_table where " + seconds + " >= dateday ORDER BY dateday Limit 5",null);
    return  nextTime;
}

Comment: So if table has rows that are 10 and 5 minutes in the past, and 3 and 7 minutes into the future, the "sort dates based on how close they are to **now**" should sort them: 3 (future), 5 (past), 7 (future), 10 (past)? If not, then you just want a normal sort, and "close to now" has no meaning.
Comment: Is `dateday` in milliseconds? Why did you assign a variable named "seconds" to milliseconds?
Comment: This Question is not clear. Are you dealing with past, present, and future moments? If so, please address the comment by Andreas.
Answer: Assuming dateday is seconds, then you can do this entirely in SQL (you don't need a Java calendar).
This query says find first 5 elements that are before now.
SELECT *
FROM timemanager_table
WHERE dateday <= strftime('%s', 'now')
ORDER BY dateday DESC
LIMIT 5

In other words, see SQLite Date & Time functions
If dateday is in some other format (like YYYY-MM-DD), you will need to strftime it, probably.

If you need to sort based on "close to now", then you need to do some date subtraction.
SELECT *, dateday - strftime('%s', 'now') AS time_diff
FROM timemanager_table
ORDER BY time_diff DESC
LIMIT 5

Which will add an additional column with negative values for those in the past, 0 for now, and positive values for those in the future.

Sqlite order by time closest to now


How to change the dynamic resources in the button click?

Question: I am new to WPF i need to learn the static resources and dynamic resources, i have tried to set the Style for Button using Dynamic Resource and change the style using button click but style is not changed, please refer my below code i used for change the button style using click,
<Window.Resources>

</Window.Resources>
<Button x:Name="Content" Content="Content" Height="30" Width="200" Style="{DynamicResource ResourceKey=Style1}" Click="Content_Click" />

Click :
 private void Content_Click(object sender, RoutedEventArgs e)
 {
   this.Resources.Add("Style1", new SolidColorBrush(Colors.Blue));
 }

Could you please any one guide me how to achieve my requirement?
Answer: Add like this hence you can overwrite the Style1 with the new style:
private void Content_Click(object sender, RoutedEventArgs e)
 {
       Style style = new Style{ TargetType = typeof(Button)};
       style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.Blue));
       this.Resources["Style1"] = style;
 }

Answer: To gain access to the Resource of the code must identify them in the file App.xaml:
<Application.Resources>
    <SolidColorBrush x:Key="Style1" />
</Application.Resources>

The Resource can be changed in code line of the form:
Application.Current.Resources["MyResource"] = MyNewValue;
private void Content_Click(object sender, RoutedEventArgs e)
 {
    SolidColorBrush MyBrush = Brushes.Blue;

    // Set the value
    Application.Current.Resources["DynamicBG"] = MyBrush;
}

Answer: If you are learning DynamicResource, the correct way would be
Xaml:
<Window.Resources>
        <SolidColorBrush x:Key="DynamicBG" Color="Red"/>
</Window.Resources>

<Button x:Name="button" Content="Content" Height="30" Width="200" Background="{DynamicResource DynamicBG}" Click="Content_Click"/>

Code Behind:
private void Content_Click(object sender, RoutedEventArgs e)
{
   this.Resources["DynamicBG"] = new SolidColorBrush(Colors.Blue);
}

Additional Note:
You can see that, on changing the Button with Background="{StaticResource DynamicBG}"will not change the Color on Buttonclick.  That's where the Dynamic key play its role.


Falha ao obter um arquivo .txt para download

Question: Tenho um método onde após uma consulta, crio um arquivo .txt salvando alguns parâmetros. Minha necessidade é obter esse .txt, onde vou tratar esse arquivo em um JavaScript (angularJs) para efetuar o download.
Mas ao tentar obter esse arquivo, tenho um retorno com o erro:

exceptionMessage: "O processo não pode acessar o arquivo 'E:\Projetos\nfs-e\Api\TEXT\users.txt' porque ele está sendo usado por outro processo."

Back end onde crio e salvo o arquivo .txt:
public HttpResponseMessage ObterNotas(UsuarioDTO user)
{
    var dataInicial = user.CompetenciaInicial;
    var dataFinal = user.CompetenciaFinal;

    var listaNotas = this.DbContext.ObterNotasRepo(dataInicial, dataFinal);

    string path = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory.ToString());

    string new_dir = path + "/TEXT/"+ "users.txt";

    HttpResponseMessage response = null;

    if (!File.Exists(new_dir))
    {
        response = Request.CreateResponse(HttpStatusCode.Gone);
    }

    MemoryStream ms = new MemoryStream();

    List<string> lista = new List<string>();
    using (TextWriter writer = new StreamWriter(new_dir))
    {
        for (int i = 0; i < listaNotas.Count ; i++)
        {
            if (i == 0)
            {
                writer.Write(listaNotas[i].Usuario.CpfCnpj.ToString() + '|' + listaNotas[i].Usuario.RazaoSocial.ToString() + "\r\n");
            }
           writer.Write(listaNotas[i].NumeroRegistro.ToString() + "\r\n");
        }
        writer.Flush();

        response.Content = new ByteArrayContent(new_dir);
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        response.Content.Headers.ContentDisposition.FileName = new_dir;

    }
    return response;
}

JavaScript:
$scope.obterUsuario = function ()
{
    $scope.search.prestadorId = authService.authentication.user.codigo;
    userService.getUser($scope.search).then(function (result) {
      if (result.data.length > 0) {
        var data = result.data
        console.log(typeof (data));

        var headers = result.headers;
        headers = headers();

        var timeInMs = Date.now();

        var contentType = headers['content-type'];

        var linkElement = document.createElement('a');
        try {
          var blob = new Blob([data], { type: contentType });
          var url = window.URL.createObjectURL(blob);

          linkElement.setAttribute('href', url);
          linkElement.setAttribute("download", "teste" + '-' + timeInMs + ".txt");

          var clickEvent = new MouseEvent("click",
            {
              "view": window,
              "bubbles": true,
              "cancelable": false
            });
          linkElement.dispatchEvent(clickEvent);
          $modalInstance.dismiss('cancel');
        } catch (ex) {
          console.log(ex);
        }
      }
    });
}

Comment: Ao que parece é o que a mensagem informa que o arquivo está sendo usado por algum outro processo, veja se você não esqueceu ele aberto em outro programa, VS, Notepad e talvez até algum anti-virus
Comment: `ms.Close();`, mas não entendi nem pra que o `MemoryStream`...
Comment: @LeandroAngelo, ele n está aberto em nenhum programa
Comment: Por que não usa `File.AppendAllText`? Pelo código, me parece que você tem um background com Java.
Comment: Em que linha ocorre o erro?
Comment: Coloque o código relativo ao objeto `response` fora do `using`. O ficheiro só é corretamente fechado depois de ser feito o `Dispose` do objeto (que faz também o `Close()` do `StreamWriter`).
Comment: Alias, qual é o objetivo de persistir em um arquivo de texto o conteúdo que está no banco de dados, como você vai tratar a concorrência de IO desse arquivo em processos "simultâneos"? Outro ponto, um txt com o mimetype de pdf, não será um arquivo pdf válido
Comment: @LeandroAngelo, só preciso obter esse arquivo .txt salvo na pasta definida. Quanto ao objetivo, são por outras razões que preciso persistir em um arquivo.
Comment: Vou retornar o total que obtive na consulta em forma de .txt
Answer: Bom a sua prática não faz muito sentido e a persistência do conteúdo em um arquivo de texto antes da resposta, que será sobrescrito a cada execução e  só serve para lhe trazer problemas de concorrência.
Além do teste para ver se o arquivo existe, você não está lendo ele em nenhum outro lugar e no final você está retornando um arquivo txt como se fosse um pdf, o que só vai gerar um arquivo inválido.
Segue o seu código com uns pequenos ajustes, apenas para evitar algumas exceptions e segregar minimante as operações de escrita e leitura, que não deveriam estar nem no mesmo método. Mas realmente sugiro que você reavalie a sua solução.
public HttpResponseMessage ObterNotas(UsuarioDTO user)
{
    var dataInicial = user.CompetenciaInicial;
    var dataFinal = user.CompetenciaFinal;

    var listaNotas = this.DbContext.ObterNotasRepo(dataInicial, dataFinal);

    string path =  Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory.ToString());

    string new_dir = path + "/TEXT/" + "users.txt";

    HttpResponseMessage response = new HttpResponseMessage();

    if (!File.Exists(new_dir))
    {
        response = Request.CreateResponse(HttpStatusCode.Gone);

    }

    List<string> lista = new List<string>();
    using (TextWriter writer = new StreamWriter(new_dir))
    {
        for (int i = 0; i < listaNotas.Count; i++)
        {
            if (i == 0)
            {
                writer.Write(listaNotas[i].Usuario.CpfCnpj.ToString() + '|' + listaNotas[i].Usuario.RazaoSocial.ToString() + "\r\n");
            }
            writer.Write(listaNotas[i].NumeroRegistro.ToString() + "\r\n");
        }
        writer.Flush();

    }

    var content = File.ReadAllBytes(new_dir);
    response.Content = new ByteArrayContent(content );
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text");
    response.Content.Headers.ContentDisposition.FileName = new_dir;

    return response;
}


Is there a site, or program, that can calculate how many total notes are in a composition?

Question: I'm new to music. It's very interesting to me how musicians can memorize so many pieces. I'm a mathematics student and naturally I'm interested in quantifying things.
To keep things simple, lets say we have one movement from a sonata, for example the first movement of Bach's violin sonata 1.
I'm sure most compositions are now "typed" up into some music composition software. I'm interested in seeing how many actual notes are in a composition. Is there a site or program that can compute this?
Comment: I cannot answer your question, but I would like to ask what information you think you would get from that number. The number of notes in a piece has absolutely nothing to do with how hard or easy it is to memorize.
Comment: To @Matt_L.'s point the number of notes won't tell you much. Music has repeating patterns or sections that help musicians memorize a piece. Also how will you count notes that are played simultaneously; as one note or more? For example if you play a 7th chord with a melody on top is that 5 notes or 1?
Comment: This reminds me of Amadeus… "Too many notes." ;)
Comment: No-one remembers all the notes as individual entities - you remember patterns.
Comment: @MattL. Just out of curiosity like I mentioned in the post.
Comment: Most classical pieces aren't memorised; they're played from sheet music.
Comment: Easily available from modern printed editions is the number of bars as a rough kind of length indication. But I have never seen a reference to it, so it seems a pretty useless indicator.
Comment: @guidot It's data that can be used by musicologists or data scientists or machine learning algorithms, like any other, even if it doesn't have an obvious, intuitive, use by the average musician.
Comment: If there are MIDI versions of the composition, then the notes might probably be easy to count too.
Comment: What would you even **define** as a note?  consider a half-note vs.  two quarter-notes, same pitch, tied together.   Or consider a turn on a note.   and so on.
Comment: Yeah! Are drum hits notes? Are you going to define a note as ending when the sound is no longer audible? Because if you do, you have to define audible, and that raises a bunch of questions... If multiple of the same instrument play the same note, is that multiple notes (string section playing one note on the staff)?
Comment: I'm voting to close this question as off-topic because finding off site resources is off topic.
Answer: One possible tool for such musicological calculations is the music21 library for Python http://web.mit.edu/music21/
Comment: +1 - I'd just add that while the question mentions music that is "typed up" in software, the specific format matters in terms of the ability to extract information and then analyze it using music21 (or other analysis packages).  Music21 can take a number of different types of input files (and there may be conversion or export commands from other software that might be able to get the music in a possible format for analysis), but just because music is "typed up" doesn't necessarily mean it's in a format that other software can parse.
Comment: @Athanasius Once you start using music21, you'll quickly notice what import formats work. :)
Comment: Yeah, I know.  I've used it.  I'm just noting that it's something OP probably needs to realize, which wasn't mentioned in your answer.
Comment: This answer would be more useful if you provided a little bit of info on what this Python code considers to be a "note" vs. "not a note"


PHP Use alternate class if present

Question: I'm trying to covert old code that uses functions to use classes. Some of the old code has options only available if an advanced option is set. In the base class I have put all of the functions (methods). If the function is available as an advanced option it is overriden in the second class. If not, it should say not available in the base class. The problem is that I can't figure out how to call one class or the other, short of putting in a bunch of if's, of course.
The original function would look like this
    function Testclass() {
     if (advanced_enabled)
       return 'Do advacned stuff
';
     else
       return 'Do base stuff
';
    }

Here are my classes:
    class A {
      public function Testclass() {
         return 'in base class
';
      }

      public function SomeBaseCode() {
      }
    }

    class B {
      public function Testclass() {
         return 'in advanced class
';
      }
    }

If I do this:
    $a = new A();
    echo 'base '.$a->Testclass();
    $b = new B();
    echo 'base '.$b->Testclass();

The output is
    in base class
    in advanced class

What I'm wanting to do is have the advanced class used if present. But the base class has to be present because it has methods always available. I can do this
    $a = new A();
    echo 'base '.$a->Testclass();
    if (advanced_enabled) {
      $b = new B();
      echo 'base '.$b->Testclass();
    }

But that gives me two different class variables and I would have to edit a lot of code to check each. I'm fairly new to classes so maybe I am missing some basic idea. Is there a way to do this?
Answer: Your advanced class B needs to extend from the base class A.
class A {
  public function Testclass() {
     return 'in base class
';
  }

  public function SomeBaseCode() {
  }
}

class B extends A {
  public function Testclass() {
     return 'in advanced class
';
  }
}

Instantiation is based on the advanced_enabled flag.
$a = advanced_enabled ? new A() : new B();
$a->SomeBaseCode();
$a->Testclass();

Answer: Instead of directly creating the class using new, use a Factory to do this for you. This factory can be a different class, or a simple function. If you use any PHP framework, you're probably using dependency injection, which can take care of this for you.
If you do not want to re-define all methods (i.e. some methods in B should be exactly the same as in A), you can use the extend keyword to have class B inherit all methods from class A you did not override. This principle is called object inheritance.
In pseudo-PHP, it would look a bit like this:
// Define classes (interface is optional, but recommended)

interface someInterface {
    public function testClass();
    public function someOtherFunction();
}

class simpleVersion implements someInterface {
    public function testClass() { /* ... */ }
    public function someOtherFunction() { /* ... */ }
}

class advancedVersion extends simpleVersion {
    public function testClass() { /* ... */ }
    // someOtherFunction is not defined here. but is still usable!
}

// Create the factory responsible for instantiating the class

function createVersion() {
  return $advancedEnabled ? new advancedVersion(); : new simpleVersion();
}

// Create the class instance (dynamically) and use it

$class = createVersion(); // $class is now either simpleVersion or advancedVersion.
$class->testClass();

Also, if you want a class B method to do something 'advanced', while also keeping the functionality of class A, you can use the special parent::something(); call to 'copy' the functionality of the base class:
class Shouter() {
    public function shout($text) {
        return strtoupper($text);
    }
}

class LoudShouter() extends Shouter {
    public function shout($text) {
        return parent::shout($text) . '!!!';
    }
}

$text = 'Hello World';

$shouter1 = new Shouter();
$shouter2 = new LoudShouter();

var_dump(
    $text,
    $shouter1->shout($text),
    $shouter2->shout($text)
);

// Output:
// -> Hello World
// -> HELLO WORLD
// -> HELLO WORLD!!!

PHP Use alternate class if present


Finding remainders of large number

Question: This question was asked in a competitive exam.

Find the remainder of dividing $6^{83}+ 8^{83}$ by $49$

Are there any theorems/rules to compute the answer ?
Comment: Maybe these [examples](http://math.stackexchange.com/search?q=remainder) can help you...
Comment: Did you mean the remainder of (6^83+8^83)/49?  Also, what exam was this taken from and is it still ongoing?
Comment: @Mike : Yes.edited the  question.
Answer: Since $6$ and $8$ are both coprime to $49$, we can apply Euler's theorem. We have $\varphi(49)=7^2-7=42$, so we have
$$(6\times 8)(6^{83}+8^{83}) \equiv 6+8\equiv 14\mod 49$$
and observing that $6\times 8\equiv -1\bmod 49$ we get that
$$6^{83}+8^{83} \equiv (-1)^{-1}(6\times 8)(6^{83}+8^{83})\equiv -14\equiv 35\mod 49$$
thus the remainder is $35$.
Answer: Use the formula for $(a+b)^n$ for $(7-1)^{83}+(7+1)^{83}$
Answer: Using Euler's Totient theorem,
As $(6,49)=1$  and $\phi(49)=42$
$6^{42}\equiv 1\pmod {49}\implies 6^{42.k}\equiv1\pmod {49}$ where is any integer.
$\implies 6^{84}\equiv 1\pmod{49}\implies 6^{83}\equiv 6^{-1}\pmod{49}$
By observation, $6\times 8=48\equiv -1\pmod{49}\implies 6\times (49-8)\equiv 1\pmod{49}\implies 41\equiv 6^{-1}\pmod{49}$
Similarly, $8^{84}\equiv 1\pmod{49}\implies 8^{83}\equiv 8^{-1}\pmod{49}$ as $(8,49)=1$
As, $6\times 8=48\equiv -1\pmod{49}$,  so $(49-6)\times 8\equiv 1\pmod {49}\implies 43\equiv 8^{-1}\pmod{49}$
So, the remainder be  $43+41=84\equiv 35\pmod{49}$.
Comment: How does $48\equiv -1\pmod{49}\implies 6\times (49-8)\equiv 1\pmod{49}$?
Comment: If ab≡-1(mod m), then a(m-b)=am-ab≡-ab(mod m)≡-(-1)=1
Answer: $\begin{eqnarray}\rm{\bf Hint}\ \ \ mod\ \phi(49)\!\!&:&\rm\,\ \color{#C00}{83}\equiv -1\ \ \  by\ \ \ \phi(49) = \phi(7^2) = 7\cdot 6 = 42_{\phantom{\frac{c}{c}}}\\
\Rightarrow\ \rm mod\ 49\!\!&:&\,\ 6^{\color{#C00}{83}}\!+\,8^{\color{#C00}{83}}\equiv\, \dfrac{1}6+\dfrac{1}8\,\equiv\,\dfrac{8+6}{8\cdot 6}\,\equiv\, \dfrac{14}{-1\ }\,\equiv\, 35\end{eqnarray}$
$\begin{eqnarray}\rm and\ \ \ \ mod\ \phi(p^2)\!\!&:&\rm\,\ \color{#C00}{\bf K}\equiv -1\\
\Rightarrow\ \rm mod\ p^2\!\!&:&\rm\,\ (p\!-\!1)^{\color{#C00}{\bf K}}\!+\!\,(p\!+\!1)^{\color{#C00}{\bf K}}\!\equiv \dfrac{1}{p\!-\!1}\!+\!\dfrac{1}{p\!+\!1}\equiv\dfrac{2p}{p^2\!-\!1}\equiv \dfrac{2p}{-1\ }\equiv p(p\!-\!2)\equiv \phi(p)\!-\!p\end{eqnarray}$

Finding remainders of large number


Determining counts of semiquavers that are joined to quavers

Question: I was given a 4/4 bar, and I'm trying to figure out what's the exact time at which to play the notes using these counts: 1-e-&-a-2-e-&-a-3-e-&-a-4-e-&-a
My attempt is written in grey below. I'm particularly confused about the semiquavers that are joined to the quavers.
Comment: So you were given the rhythmic notation, and asked to provide the "1 e" stuff? Be aware, this is not written in a standard way. The first beam (horizontal line) currently contains two quavers and a semiquaver—that's more than one beat. Beams don't usually break in the middle of beats. There's a possibility the notation is actually wrong; e.g., maybe the third note should be a semiquaver, and the 5th one should be dotted?
Comment: @AndyBonner This was what I was given. It could very well be wrong as you've mentioned (or at the very least confusing, as PiedPiper mentioned). The hint I was given is that the 1,2,3,4 above are correct, and it was up to me to figure out the Es, &s, and As.
Comment: Hm—there's definitely a mistake, then, because there are more notes coming before the "2" than will fit. If this is an assignment, I'd ask for clarification.
Comment: The person who gave the assignment (and the hint) is not the same person who created the exercise. So more than one thing could be wrong... I'll check back with them next week. Thank you!
Answer: The measure is confusingly notated. If you change the third note from an eighth to a sixteenth followed by a sixteenth rest then it will become much easier to understand (this appears to be a percussion part so rewriting it this way makes no musical difference). Once you've done this the '2' falls on the rest, the 'e' on the sixteenth. The rest of the beats (1, 3 and 4) you've understood correctly.
<code>X:1
L:1/16
K:perc
%
c2FF z Fc2 cFF2 c2e2
w: 1 & a e & 3 e & 4 &
</code>
Comment: (For completeness: if this weren't a percussion part, then an exact rewrite would have a tied semiquaver in place of the rest.)
Comment: @PiedPiper In this case, yes — but neither the title nor the body of the question specify that, and people may arrive here looking for info about non-percussion parts.  While it's good to help one person with one particular circumstance, it's even better if we can help lots more people in future too!
Comment: @gidds But it **is** a percussion part.
Comment: Thanks! You are absolutely spot on, PiedPiper. (I've since verified it with the person who came up with the original notation)


Skydrive login in iPhone App crashes

Question: I'm developing an app where I have integrated sky drive framework. But when I try to login my app crashes with:
uncaught exception 'NSInternalInconsistencyException
Reason:
Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/
Using the "LiveSDK/Library" it works fine but the library does not work with ARC.
What am I doing wrong?
Here is my code:
(void)signInMethod {
self.liveClient =
[[LiveConnectClient alloc] initWithClientId:APP_CLIENT_ID
                                                     delegate:self
                                                    userState:@"initialize"];
}

-(void)authCompleted:(LiveConnectSessionStatus) status
          session:(LiveConnectSession *) session
        userState:(id) userState { if ([userState isEqual:@"initialize"]) {
    [self.liveClient login:self
                    scopes:[NSArray arrayWithObjects:@"wl.signin", nil]
                  delegate:self
                 userState:@"signin"];

} if ([userState isEqual:@"signin"]) {
    if (session != nil)
    {
        NSLog(@"signed in");
    }
     } }

-(void)authFailed:(NSError *) error
     userState:(id)userState { NSLog(@"%@",[NSString stringWithFormat:@"Error: %@", [error localizedDescription]]);  }

Answer: The error message basically says that you have a ViewController that is trying to load a NIB file, but it cannot find it because it either is not included in the resources or it has a different name than expected.
The full error message should contain the name of the file - as you did not post it I can't know for sure, but LiveAuthDialog_iPador LiveAuthDialog_iPhone might be missing here, those are the only .xib files I've found used by the LiveSDK.
Remove the reference to the .xib file and re-add it to your project (if it exists), otherwise add it to your project. Then Clean and Build again.
(see also: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle)
Update (see comments):
In case the compiled library version ('LiveSDK.framework') is used (instead of importing the LiveSDK sources), the framework expects to be included in the app bundle's Resources folder, either by adding it to the Copy Bundle Resources build phase of the host project, or by adding it to a Copy Files Build Phase with the destination set to Resources.
The placement is important b/c the LiveSDK's -[LiveAuthRequest authorize] method will primarily try to load the NIB files for the authentication dialog from the framework bundle (using -[LiveAuthHelper getSDKBundle]), expecting the framework to be found inside the main bundle's resources:
+ (NSBundle *) getSDKBundle
{
    NSString *sdkPath = [[NSBundle mainBundle] pathForResource:@"LiveSDK"
                                                        ofType:@"framework"];
    return (sdkPath)? [NSBundle bundleWithPath:sdkPath] : [NSBundle mainBundle];
}

Comment: Did you add the LiveSDK.framework to your project's "Copy Bundle Resources" build phase?
Comment: There is a file called LiveAuthDialog_iPhone.nib in LiveSDK.Framework/Resources. How can I make my application use it? In my application, the code that try to login is not ViewController, it is NSObject class, I'm using UINavigationController.visibleViewController for the login first parameter.
Comment: Great, happy to help! :) Please remember to mark the answer you found most helpful as accepted as that will also help others encountering the same problem. Cheers
Comment: mvanallen - can you add your comment as an answer instead of a comment?
Comment: RGregg - you're right, I've updated the answer accordingly; thanks for the hint.
Answer: YOU NEED TO REGISTER YOUR NIB FILE IN ViewDidLoad. If its tableViewCell then use below code
[self.tableView registerNib: withCellIndentifier];
You you are trying to load View nib then
YourViewController *myview = [UIViewController alloc] initWithNib:];
If the nib file is itself missing then to to Target-> build phase-> Copy bundle resource and add your missing nib file

Skydrive login in iPhone App crashes


Changing static memory to dynamic C++

Question: So recently i used static memory allocation in my program but now am trying to convert it into dynamic memory, but this error occurs :
Ship.cpp(42,31): error C2061:  syntax error: identifier 'engines'

why does it need an identifier, if it is just accessing the array of objects in class Engines ? The goal is to iterate the array of engines received as a parameter and copy each element.
some codes from my  Ship.cpp:
    Ship::Ship()
    {
        setShipEmpty();
    }
    void Ship::setShipEmpty()
    {
        m_type[0] = '\0';
        m_engCnt = 0;
        for (int i = 0; i <10; i++) {
            m_engines[i].setEmpty();

        }
    }
Ship::Ship(const char* sType, const Engine* engines, int sizeOfEngine)
    {
        if (sType != nullptr && strlen(sType) > 0 && strlen(sType) < TYPE_MAX_SIZE && engines[0].get() > 0 && sizeOfEngine < 10)
        {
            setShipEmpty();
            m_type = new char[strlen(sType) + 1];
            strcpy(m_type, sType);
            m_engCnt= sizeOfEngine ;

            for (int i = 0; i < sizeOfEngine; i++) {
                m_engines[i] = new engines; <---- what am i doing wrong here?
            }
        }
        else {
            setShipEmpty();
        }
    }

The header file for ship
    class Ship
    {
        Engine* m_engines = nullptr;
        char* m_type;
        int m_engCnt;
...
}

some codes from my  engine header
class Engine
    {
        double m_size;
        char m_type[TYPE_MAX_SIZE+1];
public:
        Engine();
        Engine(const char*,double);
        ....
}

Engine.cpp
    void Engine::setEmpty()
    {
        m_type[0] = 0;
        m_size = 0;
    }

    Engine::Engine()
    {
        setEmpty();
    }

    Engine::Engine (const char* type, double size)
    {
        setEmpty();
        strcpy(m_type, type);
        m_size = size;
    }

In the Engine class i left it static , would i also need to turn this into a dynamic or can i just leave it?
Comment: What are you hoping `m_engines[i] = new engines` will do?  Copy the pointer?  (The same one every time?) ... or create a new `Engine` from the input `engines`?
Comment: @donkopotamus i was trying to iterate the array of engines received as a parameter and copy each element
Comment: You should not use raw pointers and `new`. There are standard containers for managing dynamic memory
Answer: The problem is with the code below:
m_engines[i] = new engines;

You never declared what engines is or what it does. You also do not have a constructor for engines so you cannot create an Engines object..... It uses default constructor, usually we add * after the item to indicate that it is dynamic.
Comment: Sorry, i should of added the other code as well but there is a custom constructor that i have created, it is just that their is lots of code so i try to minimize it to the source of the problem.
Comment: Kinda hard to know if theres a problem in the custom constructor if you do not post it.....
Comment: The problem seems to come from the constructor in ship.cpp when i declared new engine , how do i go about this ? Im trying to create a dynamic memory for m_engines.
Comment: The thing is that you need to do engines() with the constructor value so you can do m_engines[i] = engines(), you need to create an object of engines and call the constructor before you can allocate space for it.....
Comment: so i did do that but it is asking for a type identifer, im not sure what that would be since it is a reference to the class Engine.
Comment: Did you fix the line through, that line is not gonna work as it stands.....
Comment: @YunfeiChen The poster certainly **has** declared what `engines` is, its an `Engine*` ... in this context it needs to be treated as an array and presumably copied (`Engine(engines[i])`)
Comment: Please note that C++ is case sensitive, engines is not the same is Engine......
Comment: the error is this : Ship.cpp(42,31): error C2061:  syntax error: identifier 'engines'
Comment: i've updated the post to display both default constructor from class Engine and class Ship.
Comment: I am asking about the engines constructor not the Engine and Ship constructor, where did you define this: engines object???
Comment: sorry, i figured it out " Engine *m_engines = new Engine[sizeOfEngine]

Changing static memory to dynamic C++


SYN Flood Advice

Question: Today I've been dealing with a server suffering from what looked like a SYN flood attack. It was a bit of a rush to get the site back online, so we did these three steps to bring the service back to a usable state. The server load was low during the attack, so it didn't bring the server down, it just timed out HTTP visitors.
Now I don't believe these solved the issue, but they certainly resolved the symptons until the flood subsided.

Set sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_recv=5
Increased Apache prefork
ServerLimit and MaxClient to 512 (from 256).
Set Apache ListenBackLog to 1024

I saw various iptables --limit options being discussed elsewhere on the web, however we concluded that these would limit legitimate traffic, as each item of the webpage being requested (each image etc) would count towards this limit, stopping the page from loading fully.
What do people do in these situations, and were our action wise since load wasn't an issue?
Answer: I would use a firewall at the network perimeter to prevent\remediate SYN flood attacks (as well as DOS, DDOS, spoofing, port probes, address space probes, etc.). I don't want this type of stuff getting into my internal network, where I'll have to deal with it on a machine by machine basis.
Comment: Agreed. In this case I was left to handle it on the server as the network config couldn't be adapted in time.
Answer: Since I am not an expert in iptables, I generally let one of two firewalls handle this for me. Both APF and CSF are great firewalls when it comes to protection from SYN attacks, as well as a multitude of other ways folks can attack your server.
I do not know your specific configuration, but I have used both of the said firewalls on "general" cPanel/DirectAdmin/Plesk servers as well as some with custom services and it works great once you allow the right ports.
Separately, you may wish to turn on SYN Cookies, which helps mitigate attacks where SYN is left open. Both of the above scripts have this as an option.
Comment: Any suggestion on APF vs. CSF ? +1 for your suggestion anyway
Comment: I've used APF on many servers in the past, but CSF seems to be better developed now and has some features APF is lacking in the non-firewall, overall security of the server. If you are looking for a simple firewall APF will work well, but if you are looking for a "package" then go with CSF.
Comment: On the matter of SYN cookies - we checked and they were switched on, but we still had to take the steps above to get the site responding again. I'll have a look at those firewalls you mention, as I'm familiar with IPtables but not the advanced usage of it.

SYN Flood Advice


How to enable or disable node in CTreeCtrl in MFC?

Question: I have searched through internet & found that there are no any direct method that disable nodes of CTreeCtrl control.
check one post at http://www.ucancode.net/faq/MFC_CTreeCtrl-CListCtrl.htm & also on codeguru.com but not clear about how to disable node of CTreeCtrl.
Answer: Well, I think, the article you linked, speaks the truth: It's not possible (at least not for Windows versions before Vista, see below). You have to program the workarounds recommended in the link to "simulate" item disabling. It means: Give the item a specific colour (light grey for instance) and catch all the events which can occur on a TreeView item and cancel the actions (like expanding/collapsing a node, and so on).
Here, http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/3350ba1e-1fcf-47fe-ab6b-e20c6b5afd91, the statement of the article is more or less confirmed by Microsoft experts. They recommend the same workaround (and actually link to the same article). Also note the link to http://www.codeproject.com/KB/tree/colortreectrl.aspx on how to change individual item colors which might help if you really want to start programming the workaround.
"More or less confirmed" means: There seems to be an exception for Vista and higher:

"CTreeCtrl::SetItemStateEx()  supports TVIS_EX_DISABLED (described in TVITEMEX structure) style which disables treectrl node. But its only supported from Vista onwards."

I've seen that sometimes it is talked about a possible value TVIS_DISABLED of the state flag of the TVITEM structure and actually the MSDN of the old VC6 compiler contains a technical article (from 1994 before release of Windows 95 ;)) which says that such a state value should exist with the meaning: "The item is disabled and is drawn using the standard disabled style and color." This article was preliminary ("Please note that this article is based on preliminary information that is subject to change before the final version of Windows 95.") and obviously this value for item disabling did not make its way into the final release of Windows95 as you cannot find it in any VC header files.
So either you can make sure that your program is only used on Vista or higher computers (then it's probably easy with the extended state flag above) or it will be an ugly work (though doable and not necessarily very difficult).
Comment: +1 This answer and links helped me. However the following link helped the most and was arrived at via the links above.. http://www.codeguru.com/forum/showthread.php?t=231234 Hope this also helps others with disabling tree items on Windows versions before Vista. Cheers
Comment: @Jacob: I remember this article, but it's basically the same as the article liked in the question above. The CodeGuru article is better formatted though ;)


In R, how do I test $H_0: \beta_1+\beta_2=0$

Question: I am estimating the following model using the lm package in R:
$y_t = \alpha + \beta_1x_t + \beta_2 z_t$.
I am interest in the null hypothesis $H_0: \beta_1 + \beta_2=0$.
How can I test this?
Answer: If $\beta_1 + \beta_2 = 0$, then $\beta_1 = -\beta_2$, so $\beta_1 x_t + \beta_2 z_t = \beta_1 x_t - \beta_1 z_t = \beta_1 (x_t - z_t)$. So, in R, you can run
f1 <- lm(y ~ I(x - z), data = data)
f2 <- lm(y ~ x + z, data = data)
anova(f1, f2)

which will give you a test if the model where $\beta_1 + \beta_2 = 0$ (i.e., f1) fits worse than a model whether $\beta_1$ and $\beta_2$ can freely vary (i.e., f2). If the comparison is significant, then f2 is the better model and you can reject the null hypothesis that $\beta_1 + \beta_2 = 0$.
More generally, you can use the multcomp package to test general linear hypotheses:
multcomp::glht(f2, linfct = "x + z = 0")

Comment: +1 for the `multcomp` recommendation, a great package!
Comment: Thank you so much, Noah! Especially for the multcomp package, really helped me out.
Answer: Great thread which generated some great answers - though I have a feeling it will be moved to Stack Exchange because it is software related (the software being R).
To supplement Noah's answer, I will show an alternative way one can test the hypotheses of interest using the multcomp package.  [Note that we can't test just a null hypothesis - we need to specify an alternative hypothesis as well.]
Recall that the linear regression  model is:
$y_t = \alpha + \beta_1x_t + \beta_2 z_t$,
while the two competing hypotheses being tested are:
$H_0: \beta_1 + \beta_2 = 0$ and
$H_a: \beta_1 + \beta_2  \neq 0$.
Here, $H_0$ refers to the null hypothesis and $H_a$ refers to the alternative hypothesis.
Step 1:
The first thing we have to note is that the left hand side of the two stated hypotheses is nothing but a linear combination of the regression coefficients  $\alpha$, $\beta_1$ and $\beta_2$in the linear regression model.  Specifically:
$\beta_1 + \beta_2 = 0*\alpha + 1*\beta_1 + 1*\beta_2$,
where the weights given to the regression coefficients $\alpha$, $\beta_1$ and $\beta_2$ in this linear combination are 0, 1 and 1, respectively.
Step 2:
We will define a matrix of weights W with a single row, which lists the weights of the regression coefficients:
W <- rbind(c(0, 1, 1))
This is what W looks like currently:
> W
     [,1] [,2] [,3]
[1,]    0    1    1

Step 3:
We assign proper names to the row and column of weights W.  The row name will be beta1 + beta2.  The column names will be alpha, beta1 and beta2.  This is just so that we can keep track of what linear combination of the coefficients $\alpha$, $\beta_1$ and $\beta_2$ we are interested in testing.
rownames(W) <- c("beta1 + beta2")
colnames(W) <- c("alpha","beta1", "beta2")
W

This is what the beautified version of W looks like:
>     W
               alpha  beta1  beta2
beta1 + beta2      0      1      1

Step 4:
We fit the model and perform the test of the null hypothesis against the alternative hypothesis:
library(multcomp)

model <- lm(y ~ x + z, data = data)

model.test <- glht(model, linfct = W)

summary(model.test)

If we generate the data with the commands below:
set.seed(1)

x = rnorm(100)
z = rnorm(100)
X = model.matrix(~x+z)
y = X%*%c(1,-2,3) + rnorm(100)

data <- data.frame(y=y, x=x)

here is what the R output would look like:
>     summary(model.test)

         Simultaneous Tests for General Linear Hypotheses

Fit: lm(formula = y ~ x + z, data = data)

Linear Hypotheses:
                   Estimate Std. Error t value Pr(>|t|)
beta1 + beta2 == 0   0.9676     0.1601   6.043 2.81e-08 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Adjusted p values reported -- single-step method)

From this output, we can see that the p-value for the t-test used to test the null hypothesis against the alternative hypothesis is 2.81e-08 (which is scientific notation for 0.0000000281).
Step 5:
To estimate the value of $\beta1 + \beta2$ and compute an associated confidence interval, we can use the command:
confint(model.test)

whose output will look like this:
> confint(model.test)

         Simultaneous Confidence Intervals

Fit: lm(formula = y ~ x + z, data = data)

Quantile = 1.9847
95% family-wise confidence level

Linear Hypotheses:
                     Estimate  lwr     upr
beta1 + beta2 == 0   0.9676    0.6498  1.2855

From this output, we can see that the estimated value of $\beta1 + \beta2$ is 0.9676 and the corresponding 95% confidence interval is (0.6498, 1.2855).
We can plot the confidence interval we computed via these commands:
par(mar=c(4,8,4,4))
plot(model.test)

Comment: You always have `\not` which will add a negating slash over the following symbol: `\not =` $\not =$, `\not <` $\not <$, `\not \approx` $\not\approx$, `\not\mapsto` $\not\mapsto$
Comment: You can get the $\neq$ sign in LaTeX using ```\neq```.
Comment: @Ben: Thank you! I edited my answer to include \neq, which I didn't know about.  (It's been centuries since I used Latex.)
Answer: The variance of $\beta_1 + \beta_2$ is $\operatorname{Var}(\beta_1) + \operatorname{Var}(\beta_2)
 + 2\operatorname{Cov}(\beta_1,\beta_2)$. Obtain the variance and covaraince from the covariance matrix and construct an appropriate confidence interval.
Here is some R code.  I'm sure there is a package to do this, but until someone posts that, this should do fine.
x = rnorm(100)
z = rnorm(100)
X = model.matrix(~x+z)
y = X%*%c(1,-2,3) + rnorm(100)

model = lm(y~x+z)

sigma = vcov(model)

var_est = as.numeric(c(0,1,1)%*%sigma%*%c(0,1,1))
betas = coef(model)

(betas[2]+betas[3]) + c(-1,1)*1.96*sqrt(var_est)

Comment: You forgot to take the square root of the estimated variance.
Comment: Whoops! I'll add that as soon as I get home
Answer: Maybe you can try a Chi-square goodness of fit ( observed -expected) for your data points, for two models, one with $\beta_1= -\beta_2$ and another one where you use $\beta_2 \in (\beta_1 - \epsilon, \beta_1 + \epsilon )$  with your choice of $\epsilon >0 $ a Real number, and check the two Chi-squared statistics using the needed degrees of freedom.
Comment: What's the reason for the downvote?
Comment: You probably did not even understand my answer , yet downvoted.
Comment: @Noah: I mean regress the _data points_ used to derive $\beta_1, \beta_2$ . But let me double-check see if I am making sense.
Comment: I changed the answer.


Does a cracked carbon fiber rib protector lose its effectiveness?

Question: For karting, many drivers wear a carbon-fiber rib protector such as K1 Race Gear Carbon Fiber Racing Rib Vest.
Does such a vest lose its effectiveness in protecting your ribs when the carbon fiber becomes cracked? If so, how much does it lose, e.g. should it be thrown out and a new one purchased?
Answer: Yes. The protection provided by such equipment (in karting and other sports) is the ability of the material to flex, thereby dispensing kinetic energy into elastic energy, and to disperse forces across a wider surface area, thereby lowering the actual force experienced by any given location.
When a vest is cracked (or potentially for other materials, warped or bent) it loses a significant portion of its ability to flex. This means more of the energy is left to be absorbed by the body, which equates to a higher likelihood and severity of injury. To a lesser extent, if a cracked component is hit, it will be less able to disperse forces across an area - because the individual fragment must take up all of the force - and if the crack itself is hit, this effect is almost negligible.
Given the importance of safety from impact in sports generally and karting particularly, you are better to abandon any noticeably damaged safety equipment and replace it (or at least, the damaged components). Even the smallest of visible cracks can indicate a structural weakness in the component that, when placed under stress, will lead to failure and a lack of adequate protection.
Comment: Hi @Nij.  Thanks for the answer.  Can you improve it by adding sources to your answer?  This is the kind of question that suggests sources are appropriate, not just an explanation.


a site use Linux Server take long time to load

Question: i get this problem first time since 5 years i use same site same script without any change .
i have a site xxxxxxx.com (php script) use a ip http://126.96.36.199 (example) , i use centos as OS for the server . i use directadmin as panel to manage site config via address http://18.104.22.168:2222
normaly i have 16cpu site can support as 60 000 visitors online without problem , sometimes when more i get message on directadmin panel that cpu reached 100% and site down so i need to restart server and waiting , i understand it's CPU issue.
But from last week , i get a weird thing , the site start to down and up and down and became unreachabl when visiotrs became more than 5000 online , also in same time the ip http://126.96.36.199 of the site not accessible when the site down also http://22.214.171.124/phpmyadmin not working , BUT http://126.96.36.199:2222 is working normal so i can login and i need to access to service monitor to restar HTTPD and the site backs after 15s and may down also after some second if more visitors , ofcourse it is not problem of CPU.
i ask server support and they said nothing wrong and all good from theire side .
my question is : is it possible this problem can became from port network ? maybe they downgrade the speed of the port network ?
is possible that hosting provider can limit speed of port 80 ?
what i don't understand is ip of the site and apache http://18.104.22.168(port 80 normal) not works when more than 5000 online , but http://184.108.40.206:2222 panel ip stay works normal when the site down . maybe hosting provider limited or downgraded speed of port 80 ?
Answer: You mention that the site is slow after you gained more than 5000 visitors online. The probable cause is that the server doesn't have enough of resources to deliver the site fast enough.
Depending on how the webpage is built, you are most likely running out of memory, CPU cycles or network bandwidth. Using chrome developer tools (ctrl-shift-I) you can get an idea exactly at what stage the browser is hanging.
If you log into the monitoring tools for your server you will probably see which resources are utilized as well.
Comment: server was reached sometimes between 60 000 and 70 000 online without any problem ( sometimes down if cpu reach 100% usage ) but with 5000 online cpu usage as i see is around 13% . normaly when ressource like CPU reach maximum i get alert on adminpanel , but in this case no alert and all resource work normal . is it possible that this problem can became if hosting company reduce or downgrade port network ?
Comment: It is possible that they have limited your bandwidth. I would check your limits from when you bought the server.
Comment: bandwith means port network speed ?
Comment: i contacted server provider and they send me test speed of server is 230 mbps download and 180 mbps upload , it that enough ?
Comment: It really depends on what the server is serving. Are you serving files that are about 20 Mbytes? Then you can only serve a few people at the same time.
Comment: its a php site , like news . no files downloads


Como importar arquivo .dat para o R

Question: Baixei um arquivo .dat que e basicamente uma matriz com dados.
No entanto, me parece que a unica forma de trabalhar com ele é fazendo o seguinte comando (Estou o usando o R e não tenho muita experiencia com o R):
   setwd("C:")
    load("dat73_1")

Depois desse comando "load" esse arquivo fica "escondido" pelo R??
O código que se faz para ler esse arquivo é o seguinte:
mylist<-c("CAN","FRA","DEU","ITA","JPN","GBR","G6")

Se eu quiser as informações referente a "CAN", é so modificar "cno in 1:7" para "cno in 1:1".
  for (cno in 1:7){
      init_date<-c(1973,3)
      start_date <- init_date+c(6,3)
      end_date<-c(2009,10)
      country<-mylist[cno]
      Cdat<-data.frame(window(ts(get(country),start=init_date,freq=12),start=start_date, end=end_date))}

Eu fiz isso e consegui recuperar esse arquivo para todos os paises do mylist e criei um novo arquivo em Excel. Mas não consigo adaptar o codigo ao arquivo em excel. Há como fazer isso?
Se eu conseguir "achar" esse arquivo .dat tbm ajudaria bastante.
OBS (Editado):
Quando utilizo o comando read.table a saida é essa (RStudio):
Comment: Para ler arquivos dat, você usa a função read.table(file). Só verifica se precisa usar outros argumentos, tais como header=T, se você quiser os nomes originais das colunas e skip=número de linhas as serem evitadas. Os arquivos dat têm geralmente informações extra, então o argumento skip serve para ignorá-las.
Comment: @José obrigada!. Eu editei o topico para mostrar a saida qdo eu uso o comando read.table. Não consigo puxar direito.
Comment: 1) De onde conseguistes este arquivo? Na fonte não havia nenhuma informação sobre separador ou como ler o arquivo? 2) O comando `load()` deve ser usado em arquivos binários do `R`com extensão `.RDA` ou `.Rdata`, não deve ser o que precisas.
Comment: Olá, @Laura. Você sabe que programa gerou esse arquivo `.data`? Pelo que vi do resultado após usar o `read.table()` me parece que seu arquivo é binário (porém não no formato `.RData`, que poderia ser lido pela função `load()`). Arquivos assim geralmente exigem um tratamento fora do R para que sejam convertidos em uma tabela de dados em formato `.csv` ou `.txt`, por exemplo. Somente depois é que podem ser lidos com a função `read.table()` dentro do R.
Comment: posta o arquivo aqui de exemplo
Answer: Você pode ler diretamente com a base do R usando read.delim():
dados <- read.delim("arquivo.dat")

Answer: Para abrir arquivos no formato ".dta" uma outra alternativa é usar o pacote haven. Em seguida, procure a função read_dta.
install.packages("haven")
library("haven")
read_dta()

Comment: `.dat` e `.dta` são formatos diferentes

Como importar arquivo .dat para o R